Speed improvements to DynamicMethod<T>
See https://github.com/dotnet/runtime/issues/45232
このコミットが含まれているのは:
1 親
c3ec5235cc
コミット
0fa80a0e7d
4ファイル変更
+42
-45
検索条件に一致するファイルはありません
@@ -1,63 +1,58 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace System.Reflection.Emit
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that can be used to easily (lol) create dynamic methods
|
||||
/// A <see langword="class"/> that can be used to easily create dynamic methods.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="Delegate"/> that will be used to wrap the new methods</typeparam>
|
||||
internal static class DynamicMethod<T> where T : Delegate
|
||||
/// <typeparam name="T">The type of <see cref="Delegate"/> that will be used to wrap the new methods.</typeparam>
|
||||
internal static class DynamicMethod<T>
|
||||
where T : Delegate
|
||||
{
|
||||
/// <summary>
|
||||
/// The owner type for new dynamic methods
|
||||
/// </summary>
|
||||
private static readonly Type OwnerType = typeof(DynamicMethod<T>);
|
||||
|
||||
/// <summary>
|
||||
/// The return type of the <typeparamref name="T"/> <see langword="delegate"/>
|
||||
/// The return type of the <typeparamref name="T"/> <see langword="delegate"/>.
|
||||
/// </summary>
|
||||
private static readonly Type ReturnType;
|
||||
|
||||
/// <summary>
|
||||
/// The types of the arguments of the <typeparamref name="T"/> <see langword="delegate"/>
|
||||
/// The types of the arguments of the <typeparamref name="T"/> <see langword="delegate"/>.
|
||||
/// </summary>
|
||||
private static readonly Type[] ParameterTypes;
|
||||
|
||||
/// <summary>
|
||||
/// Loads all the necessary <see cref="Type"/> info for the current <typeparamref name="T"/> parameter
|
||||
/// Loads all the necessary <see cref="Type"/> info for the current <typeparamref name="T"/> parameter.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Local counter for dynamic methods of type <typeparamref name="T"/> ever generated
|
||||
/// Creates a new <typeparamref name="T"/> <see langword="delegate"/> for the target owner. For performance reasons,
|
||||
/// the underlying dynamic method will be created with an extra <see cref="object"/> parameter, and then bound to
|
||||
/// a <see langword="null"/> 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 <paramref name="builder"/> 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.
|
||||
/// </summary>
|
||||
private static int _Count;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <typeparamref name="T"/> <see langword="delegate"/> for the target owner
|
||||
/// </summary>
|
||||
/// <param name="builder">An <see cref="Action"/> that builds the IL bytecode for the new method</param>
|
||||
/// <returns>A new dynamic method wrapped as a <typeparamref name="T"/> <see langword="delegate"/></returns>
|
||||
/// <param name="builder">An <see cref="Action"/> that builds the IL bytecode for the new method.</param>
|
||||
/// <returns>A new dynamic method wrapped as a <typeparamref name="T"/> <see langword="delegate"/>.</returns>
|
||||
[Pure]
|
||||
public static T New(Action<ILGenerator> 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<T>(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -83,11 +83,13 @@ namespace ComputeSharp.Shaders.Translation
|
||||
private void BuildDispatchDataLoader()
|
||||
{
|
||||
// The delegate signature is <GraphicsDevice, in T, ref D3D12_GPU_DESCRIPTOR_HANDLE, ref byte, void>.
|
||||
// Due to how DynamicMethod<T> is implemented (see notes in the XML docs for DynamicMethod<T>.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<DispatchDataLoader>.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<D3D12_GPU_DESCRIPTOR_HANDLE>(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<T>.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<byte>.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);
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace ComputeSharp.Tests.Internals
|
||||
{
|
||||
Func<int, int> square = DynamicMethod<Func<int, int>>.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<Square>.New(il =>
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
il.Emit(OpCodes.Dup);
|
||||
il.Emit(OpCodes.Mul);
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする