diff --git a/src/ComputeSharp.Shaders/Helpers/DynamicMethod{T}.cs b/src/ComputeSharp.Shaders/Helpers/DynamicMethod{T}.cs index 0d5c89c2..7a1a6ec0 100644 --- a/src/ComputeSharp.Shaders/Helpers/DynamicMethod{T}.cs +++ b/src/ComputeSharp.Shaders/Helpers/DynamicMethod{T}.cs @@ -1,63 +1,58 @@ using System.Diagnostics.Contracts; using System.Linq; -using System.Threading; namespace System.Reflection.Emit { /// - /// A that can be used to easily (lol) create dynamic methods + /// A that can be used to easily create dynamic methods. /// - /// The type of that will be used to wrap the new methods - internal static class DynamicMethod where T : Delegate + /// The type of that will be used to wrap the new methods. + internal static class DynamicMethod + where T : Delegate { /// - /// The owner type for new dynamic methods - /// - private static readonly Type OwnerType = typeof(DynamicMethod); - - /// - /// The return type of the + /// The return type of the . /// private static readonly Type ReturnType; /// - /// The types of the arguments of the + /// The types of the arguments of the . /// private static readonly Type[] ParameterTypes; /// - /// Loads all the necessary info for the current parameter + /// Loads all the necessary info for the current parameter. /// static DynamicMethod() { MethodInfo method = typeof(T).GetMethod("Invoke")!; + ReturnType = method.ReturnType; - ParameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray(); + ParameterTypes = new[] { typeof(object) }.Concat(method.GetParameters().Select(static p => p.ParameterType)).ToArray(); } /// - /// Local counter for dynamic methods of type ever generated + /// Creates a new for the target owner. For performance reasons, + /// the underlying dynamic method will be created with an extra parameter, and then bound to + /// a instance. This is to emulate the behavior of instance methods, which unlike static ones + /// don't need to go through the shuffle thunk when the delegate is invoked. This would be needed to redirect the + /// invocation to a stub that performs parameters swapping, as the Invoke method is an instance one with the parameters + /// starting from offset 1, while the target static method would expect them to start from offset 0. This solution + /// allows the delegate created for the dynamic method to avoid having a shuffle thunk at all, and with it only having + /// to replace the hidden instance argument when being invoked, resulting in much better performance. Because of this, + /// it is expected that will emit the bytecode assuming that there is an extra unused argument + /// at offset 0. As a result of this, all argument load and store opcodes will need to be shifted by one position forwards. /// - private static int _Count; - - /// - /// Creates a new for the target owner - /// - /// An that builds the IL bytecode for the new method - /// A new dynamic method wrapped as a + /// An that builds the IL bytecode for the new method. + /// A new dynamic method wrapped as a . [Pure] public static T New(Action builder) { - // Create a new dynamic method with a unique id - string id = $"__IL__{typeof(T).Name}_{Interlocked.Increment(ref _Count)}"; - DynamicMethod method = new DynamicMethod(id, ReturnType, ParameterTypes, OwnerType); + DynamicMethod method = new(string.Empty, ReturnType, ParameterTypes, true); - // Build the IL bytecode - ILGenerator il = method.GetILGenerator(); - builder(il); + builder(method.GetILGenerator()); - // Build and delegate instance - return (T)method.CreateDelegate(typeof(T)); + return method.CreateDelegate(null); } } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs b/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs index 775db0a9..955ca45e 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs @@ -97,23 +97,23 @@ namespace ComputeSharp.Shaders.Translation foreach (FieldInfo fieldInfo in fieldInfos) { // (hashcode << 5) + hashcode - il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldc_I4_5); il.Emit(OpCodes.Shl); - il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Add); // hashcode += shader.Function[#].Method.GetHashCode(); - il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Ldarg_2); il.Emit(OpCodes.Ldfld, fieldInfo); il.EmitCall(OpCodes.Callvirt, getMethodInfo, null); il.EmitCall(OpCodes.Callvirt, getHashCodeInfo, null); il.Emit(OpCodes.Add); - il.Emit(OpCodes.Starg_S, (byte)0); + il.Emit(OpCodes.Starg_S, (byte)1); } // Return the computed hash - il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ret); }); } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 61a6a8a3..17bc3c55 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -83,11 +83,13 @@ namespace ComputeSharp.Shaders.Translation private void BuildDispatchDataLoader() { // The delegate signature is . + // Due to how DynamicMethod is implemented (see notes in the XML docs for DynamicMethod.New), + // all offsets for the input arguments are shifted by one. Because of this, we have the following: // - // ldarg.0 = GraphicsDevice - // ldarg.1 = in T shader - // ldarg.2 = ref D3D12_GPU_DESCRIPTOR_HANDLE r0 - // ldarg.3 = ref byte r1 + // ldarg.1 = GraphicsDevice + // ldarg.2 = in T shader + // ldarg.3 = ref D3D12_GPU_DESCRIPTOR_HANDLE r0 + // ldarg.4 = ref byte r1 this.dispatchDataLoader = DynamicMethod.New(il => { foreach (ReadableMember member in this.capturedMembers) @@ -95,14 +97,14 @@ namespace ComputeSharp.Shaders.Translation if (HlslKnownTypes.IsKnownBufferType(member.MemberType)) { // Load the offset address into the resource buffers - il.Emit(OpCodes.Ldarg_2); + il.Emit(OpCodes.Ldarg_3); if (totalResourceCount > 0) { il.EmitAddOffset(this.totalResourceCount); } - if (!member.IsStatic) il.Emit(OpCodes.Ldarg_1); + if (!member.IsStatic) il.Emit(OpCodes.Ldarg_2); il.EmitReadMember(member); // Call NativeObject.ThrowIfDisposed() @@ -113,7 +115,7 @@ namespace ComputeSharp.Shaders.Translation // Call Buffer.ThrowIfDeviceMismatch(GraphicsDevice) il.Emit(OpCodes.Dup); - il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); il.EmitCall(OpCodes.Callvirt, member.MemberType.GetMethod( nameof(Buffer.ThrowIfDeviceMismatch), BindingFlags.Instance | BindingFlags.NonPublic)!, null); @@ -140,10 +142,10 @@ namespace ComputeSharp.Shaders.Translation } // Load the target address into the variables buffer - il.Emit(OpCodes.Ldarg_3); + il.Emit(OpCodes.Ldarg_S, (byte)4); il.EmitAddOffset(totalVariablesByteSize); - if (!member.IsStatic) il.Emit(OpCodes.Ldarg_1); + if (!member.IsStatic) il.Emit(OpCodes.Ldarg_2); il.EmitReadMember(member); il.EmitStoreToAddress(member.MemberType); diff --git a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs index a00a7726..77825c61 100644 --- a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs +++ b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.Tests.Internals { Func square = DynamicMethod>.New(il => { - il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Mul); il.Emit(OpCodes.Ret); @@ -31,7 +31,7 @@ namespace ComputeSharp.Tests.Internals { Square square = DynamicMethod.New(il => { - il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Mul); il.Emit(OpCodes.Ret);