From 7f3f0f58d10c63ff1f9f9c51dff129ebb5650d03 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Sep 2019 01:33:57 +0200 Subject: [PATCH] Fixed IL generation when using stobj --- .../Extensions/ILGeneratorExtensions.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs index 05256421..860e0a41 100644 --- a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs @@ -127,7 +127,8 @@ namespace System.Reflection.Emit { if (type.IsValueType) { - il.Emit(Marshal.SizeOf(type) switch + // Pick the optimal opcode to set a value type + OpCode opcode = Marshal.SizeOf(type) switch { // Use the faster op codes for sizes <= 8 1 => OpCodes.Stind_I1, @@ -139,7 +140,11 @@ namespace System.Reflection.Emit // Default to stobj for all other value types _ => OpCodes.Stobj - }); + }; + + // Also pass the type token if stobj is used + if (opcode == OpCodes.Stobj) il.Emit(opcode, type); + else il.Emit(opcode); } else il.Emit(OpCodes.Stind_Ref); }