diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs index 411f6cb4..c4c73783 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Contracts; using System.Linq; using System.Reflection.Emit; using System.Runtime.CompilerServices; +using ComputeSharp.Shaders.Extensions; namespace ComputeSharp.Shaders.Translation.Models { @@ -79,39 +80,35 @@ namespace ComputeSharp.Shaders.Translation.Models [Pure] private Getter BuildDynamicGetter() { - // Create a new dynamic method for the current member - DynamicMethod method = new DynamicMethod($"Get{Name}", typeof(object), new[] { typeof(object) }, DeclaringType); - ILGenerator il = method.GetILGenerator(); - - // Info for hierarchical access - ReadableMember[] hierarchy = (Parents ?? Array.Empty()).Concat(new[] { this }).ToArray(); - - // Load the argument (the object instance) and cast it to the right type, if needed - if (!IsStatic) + return DynamicMethod.New(il => { - il.Emit(OpCodes.Ldarg_0); + // Info for hierarchical access + ReadableMember[] hierarchy = (Parents ?? Array.Empty()).Concat(new[] { this }).ToArray(); - Type unboxType = hierarchy[0].DeclaringType; - il.Emit(unboxType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, unboxType); - } + // Load the argument (the object instance) and cast it to the right type, if needed + if (!IsStatic) + { + il.Emit(OpCodes.Ldarg_0); - // Unroll the member access - foreach (ReadableMember member in hierarchy) - { - // Get the member value with the appropriate method - if (member.Property != null) il.EmitCall(member.IsStatic ? OpCodes.Call : OpCodes.Callvirt, member.Property.GetMethod, null); - else if (member.Field != null) il.Emit(member.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, member.Field); - else throw new InvalidOperationException("Field and property can't both be null at the same time"); - } + Type unboxType = hierarchy[0].DeclaringType; + il.Emit(unboxType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, unboxType); + } - // Box the value, if needed - if (MemberType.IsValueType) il.Emit(OpCodes.Box, MemberType); + // Unroll the member access + foreach (ReadableMember member in hierarchy) + { + // Get the member value with the appropriate method + if (member.Property != null) il.EmitCall(member.IsStatic ? OpCodes.Call : OpCodes.Callvirt, member.Property.GetMethod, null); + else if (member.Field != null) il.Emit(member.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, member.Field); + else throw new InvalidOperationException("Field and property can't both be null at the same time"); + } - // Return the member value from the top of the evaluation stack - il.Emit(OpCodes.Ret); + // Box the value, if needed + if (MemberType.IsValueType) il.Emit(OpCodes.Box, MemberType); - // Build and return the delegate - return (Getter)method.CreateDelegate(typeof(Getter)); + // Return the member value from the top of the evaluation stack + il.Emit(OpCodes.Ret); + }); } } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs b/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs index 44829285..bea29668 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderHashCodeProvider.cs @@ -67,58 +67,54 @@ namespace ComputeSharp.Shaders.Translation [Pure] private static Hasher BuildDynamicHasher(Type declaringType, IReadOnlyCollection fieldInfos) { - // Create a new dynamic method for the current member - DynamicMethod method = new DynamicMethod("GetHashCodeForHlslDelegates", typeof(int), new[] { typeof(int), typeof(object) }, declaringType); - ILGenerator il = method.GetILGenerator(); - - // Declare the local variable to store the target instance - il.DeclareLocal(declaringType); - - // Load the argument (the object instance) - il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Castclass, declaringType); - il.Emit(OpCodes.Stloc_0); - - // Unroll the member access - MethodInfo - validateDelegateInfo = typeof(ShaderHashCodeProvider).GetMethod(nameof(ValidateDelegate), BindingFlags.NonPublic | BindingFlags.Static), - getMethodInfo = typeof(Delegate).GetProperty(nameof(Delegate.Method)).GetMethod, - getHashCodeInfo = typeof(object).GetMethod(nameof(GetHashCode)); - foreach (FieldInfo fieldInfo in fieldInfos) + return DynamicMethod.New(il => { - Label label = il.DefineLabel(); + // Declare the local variable to store the target instance + il.DeclareLocal(declaringType); - // if (fieldInfo.GetValue(obj) != null) { - il.Emit(OpCodes.Ldloc_0); - il.Emit(OpCodes.Ldfld, fieldInfo); - il.Emit(OpCodes.Brfalse_S, label); + // Load the argument (the object instance) + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Castclass, declaringType); + il.Emit(OpCodes.Stloc_0); - // if (ValidateDelegate(field)) { - il.Emit(OpCodes.Ldloc_0); - il.Emit(OpCodes.Ldfld, fieldInfo); - il.EmitCall(OpCodes.Call, validateDelegateInfo, null); - il.Emit(OpCodes.Brfalse_S, label); + // Unroll the member access + MethodInfo + validateDelegateInfo = typeof(ShaderHashCodeProvider).GetMethod(nameof(ValidateDelegate), BindingFlags.NonPublic | BindingFlags.Static), + getMethodInfo = typeof(Delegate).GetProperty(nameof(Delegate.Method)).GetMethod, + getHashCodeInfo = typeof(object).GetMethod(nameof(GetHashCode)); + foreach (FieldInfo fieldInfo in fieldInfos) + { + Label label = il.DefineLabel(); - // hash = hash * 17 + field.Method.GetHashCode(); + // if (fieldInfo.GetValue(obj) != null) { + il.Emit(OpCodes.Ldloc_0); + il.Emit(OpCodes.Ldfld, fieldInfo); + il.Emit(OpCodes.Brfalse_S, label); + + // if (ValidateDelegate(field)) { + il.Emit(OpCodes.Ldloc_0); + il.Emit(OpCodes.Ldfld, fieldInfo); + il.EmitCall(OpCodes.Call, validateDelegateInfo, null); + il.Emit(OpCodes.Brfalse_S, label); + + // hash = hash * 17 + field.Method.GetHashCode(); + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Ldc_I4, 17); + il.Emit(OpCodes.Mul); + il.Emit(OpCodes.Ldloc_0); + 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, 0); + + il.MarkLabel(label); + } + + // Return the computed hash il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Ldc_I4, 17); - il.Emit(OpCodes.Mul); - il.Emit(OpCodes.Ldloc_0); - 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, 0); - - il.MarkLabel(label); - } - - // Return the computed hash - il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Ret); - - // Build and return the delegate - return (Hasher)method.CreateDelegate(typeof(Hasher)); + il.Emit(OpCodes.Ret); + }); } /// diff --git a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs index 1537d452..42aeed8b 100644 --- a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs +++ b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs @@ -1,4 +1,5 @@ -using System.Reflection.Emit; +using System; +using System.Reflection.Emit; using ComputeSharp.Shaders.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -8,8 +9,25 @@ namespace ComputeSharp.Tests.Internals [TestCategory("DynamicMethod")] public class DynamicMethodTests { + [TestMethod] + public void TestFunc() + { + Func square = DynamicMethod>.New(il => + { + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Dup); + il.Emit(OpCodes.Mul); + il.Emit(OpCodes.Ret); + }); + + int result = square(2); // result = 2 * 2 + + Assert.IsTrue(result == 4); + } + public delegate int Square(int x); + [TestMethod] public void TestSquare() { Square square = DynamicMethod.New(il =>