diff --git a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs deleted file mode 100644 index 816911bc..00000000 --- a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Reflection; -using System.Reflection.Emit; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using Microsoft.Toolkit.Diagnostics; - -namespace ComputeSharp.Shaders.Extensions -{ - /// - /// A that provides extension methods for the type. - /// - internal static class ILGeneratorExtensions - { - /// - /// Puts the appropriate , , or instruction to read a member onte the stream of instructions. - /// - /// The input instance to use to emit instructions. - /// The member to read. - public static void EmitReadMember(this ILGenerator il, MemberInfo member) - { - switch (member) - { - case FieldInfo field: - il.Emit(field.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, field); - break; - case PropertyInfo property when property.CanRead: - il.EmitCall(property.GetMethod!.IsStatic ? OpCodes.Call : OpCodes.Callvirt, property.GetMethod, null); - break; - default: ThrowHelper.ThrowArgumentException("The input MemberInfo instance can't be read"); return; - } - } - - /// - /// 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 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 < -128) il.Emit(OpCodes.Ldc_I4, offset); - else if (offset < 0) il.Emit(OpCodes.Ldc_I4_S, (sbyte)offset); - else if (offset <= 8) - { - il.Emit(offset switch - { - 0 => OpCodes.Ldc_I4_0, - 1 => OpCodes.Ldc_I4_1, - 2 => OpCodes.Ldc_I4_2, - 3 => OpCodes.Ldc_I4_3, - 4 => OpCodes.Ldc_I4_4, - 5 => OpCodes.Ldc_I4_5, - 6 => OpCodes.Ldc_I4_6, - 7 => OpCodes.Ldc_I4_7, - 8 => OpCodes.Ldc_I4_8, - _ => ThrowHelper.ThrowArgumentException("Invalid offset value") - }); - } - else if (offset <= 127) il.Emit(OpCodes.Ldc_I4_S, (byte)offset); - else il.Emit(OpCodes.Ldc_I4, offset); - - // Converts the offset to native int and adds to the address - il.Emit(OpCodes.Conv_I); - il.Emit(OpCodes.Add); - } - - /// - /// Puts the appropriate or instruction to write to a reference onto the stream of instructions. - /// - /// The input instance to use to emit instructions. - /// 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) - { - if (type.IsValueType) - { - // Pick the optimal opcode to set a value type - OpCode opcode = Marshal.SizeOf(type) switch - { - // Use the faster op codes for sizes <= 8 - 1 when type == typeof(byte) || type == typeof(sbyte) => OpCodes.Stind_I1, - 2 when type == typeof(short) || type == typeof(ushort) => OpCodes.Stind_I2, - 4 when type == typeof(float) => OpCodes.Stind_R4, - 4 when type == typeof(int) || type == typeof(uint) => 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 - }; - - // 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); - } - } -} diff --git a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs deleted file mode 100644 index 72771697..00000000 --- a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Diagnostics.Contracts; - -namespace ComputeSharp.Shaders.Extensions -{ - /// - /// A that provides extension methods for the type. - /// - internal static class TypeExtensions - { - /// - /// Gets whether or not the input represents a . - /// - /// The input type to analyze. - [Pure] - public static bool IsDelegate(this Type type) - { - return type.IsSubclassOf(typeof(Delegate)); - } - } -} diff --git a/src/ComputeSharp.Shaders/Helpers/DynamicMethod{T}.cs b/src/ComputeSharp.Shaders/Helpers/DynamicMethod{T}.cs deleted file mode 100644 index 7a1a6ec0..00000000 --- a/src/ComputeSharp.Shaders/Helpers/DynamicMethod{T}.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System.Diagnostics.Contracts; -using System.Linq; - -namespace System.Reflection.Emit -{ - /// - /// 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 return type of the . - /// - private static readonly Type ReturnType; - - /// - /// The types of the arguments of the . - /// - private static readonly Type[] ParameterTypes; - - /// - /// Loads all the necessary info for the current parameter. - /// - static DynamicMethod() - { - MethodInfo method = typeof(T).GetMethod("Invoke")!; - - ReturnType = method.ReturnType; - ParameterTypes = new[] { typeof(object) }.Concat(method.GetParameters().Select(static p => p.ParameterType)).ToArray(); - } - - /// - /// 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. - /// - /// An that builds the IL bytecode for the new method. - /// A new dynamic method wrapped as a . - [Pure] - public static T New(Action builder) - { - DynamicMethod method = new(string.Empty, ReturnType, ParameterTypes, true); - - builder(method.GetILGenerator()); - - return method.CreateDelegate(null); - } - } -} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs similarity index 100% rename from src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs rename to src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs diff --git a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs deleted file mode 100644 index 0c1a5453..00000000 --- a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Reflection.Emit; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.Tests.Internals -{ - [TestClass] - [TestCategory("DynamicMethod")] - public class DynamicMethodTests - { - [TestMethod] - public void TestFunc() - { - Func square = DynamicMethod>.New(il => - { - // Due to the instance method trick, the first parameter is at offset 1. - // See the docs on DynamicMethod.New for more info on how this works. - il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Dup); - il.Emit(OpCodes.Mul); - il.Emit(OpCodes.Ret); - }); - - int result = square(2); // result = 2 * 2 - - Assert.IsTrue(result == 4); - } - } -}