Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7a3eaba
Convert COMCustomAttribute from MDCS to UCO
am11 Apr 4, 2026
f3e9b82
Address Aaron's feedback
am11 Apr 5, 2026
3e6efe5
Address Jan's feedback
am11 Apr 5, 2026
d7f8b33
Use emitted thunks in RuntimeMethodHandle.InvokeMethod
am11 Apr 5, 2026
be9dddc
Revert reflection stack for benchmarks
am11 Apr 5, 2026
31de3bf
Revert to 3e6efe5703f39d39934caeea5d123fd7e927bb5c
am11 Apr 9, 2026
2d313da
Merge dotnet/main into feature/MDCS-to-UCOA-pattern
am11 Apr 9, 2026
75cb0c4
Merge branch 'main' into feature/MDCS-to-UCOA-pattern
am11 Apr 10, 2026
954af8a
Remove CallDescrWorkerWith from InvokeMethod path
am11 Apr 11, 2026
3baf854
Merge dotnet/main into feature/MDCS-to-UCOA-pattern
am11 Apr 11, 2026
aa0da9d
Fix mono
am11 Apr 11, 2026
a981f2d
Update callhelpers-reverse for wasm
am11 Apr 11, 2026
da967a9
Fix mono condition
am11 Apr 11, 2026
53679e0
Skip arg slot fixup for ref-likes
am11 Apr 11, 2026
0ba9e63
Merge branch 'main' into feature/MDCS-to-UCOA-pattern
am11 Apr 11, 2026
a575025
Dedups
am11 Apr 12, 2026
86d0bd6
Merge dotnet/main into feature/MDCS-to-UCOA-pattern
am11 Apr 12, 2026
05af3bd
Exclude mono
am11 Apr 12, 2026
0eb1889
Restore comment
am11 Apr 12, 2026
2378b88
Cleanups
am11 Apr 12, 2026
6dd7e5d
More cleanups
am11 Apr 12, 2026
b26c6ae
Merge branch 'main' into feature/MDCS-to-UCOA-pattern
am11 Apr 12, 2026
0a8c91b
Merge branch 'main' into feature/MDCS-to-UCOA-pattern
jkotas May 17, 2026
552671a
Port and adapt Steve's 115345 changes
am11 May 18, 2026
dea74f6
Merge dotnet/main into feature/MDCS-to-UCOA-pattern
am11 May 19, 2026
8ea3398
Improve caching
am11 May 19, 2026
a06e0be
Merge branch 'main' into feature/MDCS-to-UCOA-pattern
am11 May 19, 2026
95b2a2f
Merge dotnet/main into feature/MDCS-to-UCOA-pattern
am11 May 19, 2026
24a5a1f
Remove RequiresUnsafe (cleaned up in main branch)
am11 May 20, 2026
ef06e02
Merge branch 'main' into feature/MDCS-to-UCOA-pattern
am11 May 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use emitted thunks in RuntimeMethodHandle.InvokeMethod
  • Loading branch information
am11 committed Apr 5, 2026
commit d7f8b33797743affb9e1c1f3053ab8749da82513
39 changes: 31 additions & 8 deletions src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,16 +1203,37 @@ internal static MdUtf8String GetUtf8Name(RuntimeMethodHandleInternal method)
[RequiresUnsafe]
internal static object? InvokeMethod(object? target, void** arguments, Signature sig, bool isConstructor)
{
object? result = null;
InvokeMethod(
ObjectHandleOnStack.Create(ref target),
arguments,
ObjectHandleOnStack.Create(ref sig),
isConstructor ? Interop.BOOL.TRUE : Interop.BOOL.FALSE,
ObjectHandleOnStack.Create(ref result));
return result;
if (!RuntimeFeature.IsDynamicCodeSupported)
{
object? result = null;
InvokeMethod(
ObjectHandleOnStack.Create(ref target),
arguments,
ObjectHandleOnStack.Create(ref sig),
isConstructor ? Interop.BOOL.TRUE : Interop.BOOL.FALSE,
ObjectHandleOnStack.Create(ref result));
return result;
}

MethodBase? method = RuntimeType.GetMethodBase(sig.DeclaringType, sig.MethodHandle);
if (method is null)
{
throw new InvalidOperationException(SR.Arg_InvalidHandle);
}

Reflection.InvokerEmitUtil.InvokeFunc_RefArgs thunk = method is Reflection.RuntimeConstructorInfo && !isConstructor
? s_invokeMethodCtorNoAllocCache.GetValue(method, static m => Reflection.InvokerEmitUtil.CreateInvokeDelegate_RefArgs(m, backwardsCompat: true, constructorWithoutAllocation: true))
: s_invokeMethodCache.GetValue(method, static m => Reflection.InvokerEmitUtil.CreateInvokeDelegate_RefArgs(m, backwardsCompat: true));

return thunk(target, (IntPtr*)arguments);
}

private static readonly ConditionalWeakTable<MethodBase, Reflection.InvokerEmitUtil.InvokeFunc_RefArgs> s_invokeMethodCache =
new();

private static readonly ConditionalWeakTable<MethodBase, Reflection.InvokerEmitUtil.InvokeFunc_RefArgs> s_invokeMethodCtorNoAllocCache =
new();

/// <summary>
/// For a true boxed Nullable{T}, re-box to a boxed {T} or null, otherwise just return the input.
/// </summary>
Expand Down Expand Up @@ -2146,6 +2167,8 @@ public Signature(void* pCorSig, int cCorSig, RuntimeType declaringType)

#region Internal Members
internal CallingConventions CallingConvention => (CallingConventions)(_managedCallingConventionAndArgIteratorFlags & 0xff);
internal RuntimeMethodHandleInternal MethodHandle => _pMethod;
internal RuntimeType DeclaringType => _declaringType;
internal RuntimeType[] Arguments
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,14 @@ public static InvokeFunc_ObjSpanArgs CreateInvokeDelegate_ObjSpanArgs(MethodBase
}

public static InvokeFunc_RefArgs CreateInvokeDelegate_RefArgs(MethodBase method, bool backwardsCompat)
=> CreateInvokeDelegate_RefArgs(method, backwardsCompat, constructorWithoutAllocation: false);

public static InvokeFunc_RefArgs CreateInvokeDelegate_RefArgs(MethodBase method, bool backwardsCompat, bool constructorWithoutAllocation)
{
Debug.Assert(!method.ContainsGenericParameters);
Debug.Assert(!constructorWithoutAllocation || method is RuntimeConstructorInfo);

bool emitNew = method is RuntimeConstructorInfo;
bool emitNew = method is RuntimeConstructorInfo && !constructorWithoutAllocation;
bool hasThis = !(emitNew || method.IsStatic);

// The first parameter is unused but supports treating the DynamicMethod as an instance method which is slightly faster than a static.
Expand Down Expand Up @@ -190,7 +194,7 @@ public static InvokeFunc_RefArgs CreateInvokeDelegate_RefArgs(MethodBase method,
}
}

EmitCallAndReturnHandling(il, method, emitNew, backwardsCompat);
EmitCallAndReturnHandling(il, method, emitNew, backwardsCompat, constructorWithoutAllocation);

// Create the delegate; it is also compiled at this point due to restrictedSkipVisibility=true.
return (InvokeFunc_RefArgs)dm.CreateDelegate(typeof(InvokeFunc_RefArgs), target: null);
Expand All @@ -205,7 +209,7 @@ private static void Unbox(ILGenerator il, Type parameterType)
il.Emit(OpCodes.Ldobj, parameterType);
}

private static void EmitCallAndReturnHandling(ILGenerator il, MethodBase method, bool emitNew, bool backwardsCompat)
private static void EmitCallAndReturnHandling(ILGenerator il, MethodBase method, bool emitNew, bool backwardsCompat, bool constructorWithoutAllocation = false)
{
// For CallStack reasons, don't inline target method.
// Mono interpreter does not support\need this.
Expand All @@ -224,6 +228,11 @@ private static void EmitCallAndReturnHandling(ILGenerator il, MethodBase method,
{
il.Emit(OpCodes.Newobj, (ConstructorInfo)method);
}
else if (method is RuntimeConstructorInfo)
{
// Constructor invocation on an existing target object.
il.Emit(OpCodes.Call, (ConstructorInfo)method);
}
else if (method.IsStatic || method.DeclaringType!.IsValueType)
{
il.Emit(OpCodes.Call, (MethodInfo)method);
Expand All @@ -242,6 +251,10 @@ private static void EmitCallAndReturnHandling(ILGenerator il, MethodBase method,
il.Emit(OpCodes.Box, returnType);
}
}
else if (constructorWithoutAllocation)
{
il.Emit(OpCodes.Ldnull);
}
else
{
RuntimeType returnType;
Expand Down