Skip to content

Improve startup perf by avoiding JIT when invoking well-known signatures #115345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodBase.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodBaseInvoker.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodInvoker.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodInvokerCommon.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\ModifiedType.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\RtFieldInfo.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\RuntimeAssembly.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.CompilerServices;
using static System.Reflection.InvokerEmitUtil;
using static System.Reflection.MethodBase;
using static System.RuntimeType;

namespace System.Reflection
{
public partial class ConstructorInvoker
{
private readonly Signature? _signature;
private readonly CreateUninitializedCache? _allocator;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private object CreateUninitializedObject() => _allocator!.CreateUninitializedObject(_declaringType);

internal unsafe ConstructorInvoker(RuntimeConstructorInfo constructor) : this(constructor, constructor.Signature.Arguments)
private bool ShouldAllocate
{
_signature = constructor.Signature;
_invokeFunc_RefArgs = InterpretedInvoke;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _allocator is not null;
}
private unsafe Delegate CreateInvokeDelegateForInterpreted()
{
Debug.Assert(MethodInvokerCommon.UseInterpretedPath);
Debug.Assert(_strategy == InvokerStrategy.Ref4 || _strategy == InvokerStrategy.RefMany);

return (InvokeFunc_RefArgs)InterpretedInvoke;
}

private unsafe object? InterpretedInvoke(object? obj, IntPtr* args)
private unsafe object? InterpretedInvoke(IntPtr _, object? obj, IntPtr* args)
{
return RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: obj is null);
return RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _method.Signature, isConstructor: obj is null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private MethodBaseInvoker Invoker
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _invoker ??= new MethodBaseInvoker(this, Signature);
return _invoker ??= new MethodBaseInvoker(this, Signature.Arguments, ReturnType);
}
}

Expand Down Expand Up @@ -132,13 +132,17 @@ Signature LazyCreateSignature()
int argCount = (parameters != null) ? parameters.Length : 0;
if (Signature.Arguments.Length != argCount)
throw new TargetParameterCountException(SR.Arg_ParmCnt);
object? retValue = argCount switch

object? retValue = Invoker.Strategy switch
{
0 => Invoker.InvokeWithNoArgs(obj, invokeAttr),
1 => Invoker.InvokeWithOneArg(obj, invokeAttr, binder, parameters!, culture),
2 or 3 or 4 => Invoker.InvokeWithFewArgs(obj, invokeAttr, binder, parameters!, culture),
_ => Invoker.InvokeWithManyArgs(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.Obj0 => Invoker.InvokeWithNoArgs(obj, invokeAttr),
MethodBase.InvokerStrategy.Obj1 => Invoker.InvokeWith1Arg(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.Obj4 => Invoker.InvokeWith4Args(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.ObjSpan => Invoker.InvokeWithSpanArgs(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.Ref4 => Invoker.InvokeWith4RefArgs(obj, invokeAttr, binder, parameters, culture),
_ => Invoker.InvokeWithManyRefArgs(obj, invokeAttr, binder, parameters!, culture)
};

GC.KeepAlive(this);
return retValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System.Reflection
Expand All @@ -15,152 +14,188 @@ internal static unsafe class InstanceCalliHelper
// Zero parameter methods such as property getters:

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do these need to be marked with NoInlining?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it, there is an AV during inlining.

So either the "dynamic method" that we created based on the intrinsic doesn't support inlining or there is a jit bug.

The exception is in the call to GetModule() here with call stack:

  Assert failure(PID 60300 [0x0000eb8c], Thread: 40924 [0x9fdc]): Consistency check failed: AV in clr at this callstack:
  ------
  CORECLR! CEEInfo::getArgType + 0x56E (0x00007ffe`20b74cfe)
  CLRJIT! Compiler::impInlineInitVars + 0x322 (0x00007ffe`36f8f0e2)
  CLRJIT! `Compiler::fgInvokeInlineeCompiler'::`2'::<lambda_1>::operator() + 0x24 (0x00007ffe`36edf994)
  CORECLR! `CEEInfo::runWithErrorTrap'::`6'::__Body::Run + 0x86 (0x00007ffe`20b6c2c6)
  CORECLR! CEEInfo::runWithErrorTrap + 0x2D (0x00007ffe`20ba27ed)
  CLRJIT! Compiler::fgInvokeInlineeCompiler + 0x34C (0x00007ffe`36ee342c)
  CLRJIT! Compiler::fgMorphCallInlineHelper + 0x2E3 (0x00007ffe`36ee3ae3)
  CLRJIT! Compiler::fgMorphCallInline + 0x54 (0x00007ffe`36ee3664)
  CLRJIT! Compiler::fgInline + 0x1C3 (0x00007ffe`36ee10d3)
  CLRJIT! Phase::Run + 0x76 (0x00007ffe`3708da86)
  CLRJIT! Compiler::compCompile + 0x57F (0x00007ffe`36e97cef)
  CLRJIT! Compiler::compCompileHelper + 0xA03 (0x00007ffe`36e9bb93)
  CLRJIT! Compiler::compCompile + 0xA18 (0x00007ffe`36e9a978)
  CLRJIT! `jitNativeCode'::`8'::__Body::Run + 0xE2 (0x00007ffe`36e96762)
  CLRJIT! jitNativeCode + 0x16E (0x00007ffe`36ea0f8e)
  CLRJIT! CILJit::compileMethod + 0x169 (0x00007ffe`36ea8759)
  CORECLR! invokeCompileMethodHelper + 0x151 (0x00007ffe`20b9cc01)
  set XUNIT_HIDE_PASSING_OUTPUT_DIAGNOSTICS=1
  CORECLR! invokeCompileMethod + 0x128 (0x00007ffe`20b9c9a8)
  CORECLR! UnsafeJitFunctionWorker + 0x2EF (0x00007ffe`20b6dccf)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks like a bug in the intrinsic implementation. It would be a good idea to understand it (and fix it). It may have other silent manifestations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look and report back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a better look at this and created issue #115429 to track. I can continue to investigate but thought I would create the issue now to see if someone has ideas or wants to pick it up.

If addressed, then we can either remove the NoInlining or replace with AggressiveInlining.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried that this bug is some kind of memory corruption, and it is still going repro - but less frequently - with the NoInlining workaround.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steveharter I will try and take a look this week.

Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix is #115639

internal static bool Call(delegate*<object, bool> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static byte Call(delegate*<object, byte> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static char Call(delegate*<object, char> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static DateTime Call(delegate*<object, DateTime> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static DateTimeOffset Call(delegate*<object, DateTimeOffset> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static decimal Call(delegate*<object, decimal> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static double Call(delegate*<object, double> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static float Call(delegate*<object, float> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static Guid Call(delegate*<object, Guid> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static short Call(delegate*<object, short> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static int Call(delegate*<object, int> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static long Call(delegate*<object, long> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static nint Call(delegate*<object, nint> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static nuint Call(delegate*<object, nuint> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static object? Call(delegate*<object, object?> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static sbyte Call(delegate*<object, sbyte> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static ushort Call(delegate*<object, ushort> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static uint Call(delegate*<object, uint> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static ulong Call(delegate*<object, ulong> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, void> fn, object o) => fn(o);

// One parameter methods with no return such as property setters:

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, bool, void> fn, object o, bool arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, byte, void> fn, object o, byte arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, char, void> fn, object o, char arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, DateTime, void> fn, object o, DateTime arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, DateTimeOffset, void> fn, object o, DateTimeOffset arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, decimal, void> fn, object o, decimal arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, double, void> fn, object o, double arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, float, void> fn, object o, float arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, Guid, void> fn, object o, Guid arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, short, void> fn, object o, short arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, int, void> fn, object o, int arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, long, void> fn, object o, long arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, nint, void> fn, object o, nint arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, nuint, void> fn, object o, nuint arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, void> fn, object o, object? arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, sbyte, void> fn, object o, sbyte arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, ushort, void> fn, object o, ushort arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, uint, void> fn, object o, uint arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, ulong, void> fn, object o, ulong arg1) => fn(o, arg1);

// Other methods:

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, void> fn, object o, object? arg1, object? arg2)
=> fn(o, arg1, arg2);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3)
=> fn(o, arg1, arg2, arg3);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3, object? arg4)
=> fn(o, arg1, arg2, arg3, arg4);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3, object? arg4, object? arg5)
=> fn(o, arg1, arg2, arg3, arg4, arg5);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3, object? arg4, object? arg5, object? arg6)
=> fn(o, arg1, arg2, arg3, arg4, arg5, arg6);

[Intrinsic]
internal static void Call(delegate*<object, IEnumerable<object>?, void> fn, object o, IEnumerable<object>? arg1)
=> fn(o, arg1);

[Intrinsic]
internal static void Call(delegate*<object, IEnumerable<object>?, IEnumerable<object>?, void> fn, object o, IEnumerable<object>? arg1, IEnumerable<object>? arg2)
=> fn(o, arg1, arg2);
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using static System.Reflection.InvokerEmitUtil;
using static System.Reflection.MethodBase;
using static System.RuntimeType;

namespace System.Reflection
{
internal partial class MethodBaseInvoker
{
private readonly Signature? _signature;
private readonly CreateUninitializedCache? _allocator;

internal unsafe MethodBaseInvoker(RuntimeMethodInfo method) : this(method, method.Signature.Arguments)
{
_signature = method.Signature;
_invocationFlags = method.ComputeAndUpdateInvocationFlags();
_invokeFunc_RefArgs = InterpretedInvoke_Method;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private object CreateUninitializedObject() => _allocator!.CreateUninitializedObject(_declaringType!);

internal unsafe MethodBaseInvoker(RuntimeConstructorInfo constructor) : this(constructor, constructor.Signature.Arguments)
private bool ShouldAllocate
{
_signature = constructor.Signature;
_invocationFlags = constructor.ComputeAndUpdateInvocationFlags();
_invokeFunc_RefArgs = InterpretedInvoke_Constructor;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _allocator is not null;
}

internal unsafe MethodBaseInvoker(DynamicMethod method, Signature signature) : this(method, signature.Arguments)
private unsafe Delegate CreateInvokeDelegateForInterpreted()
{
_signature = signature;
_invokeFunc_RefArgs = InterpretedInvoke_Method;
Debug.Assert(MethodInvokerCommon.UseInterpretedPath);
Debug.Assert(_strategy == InvokerStrategy.Ref4 || _strategy == InvokerStrategy.RefMany);

if (_method is RuntimeMethodInfo)
{
return (InvokeFunc_RefArgs)InterpretedInvoke_Method;
}

if (_method is RuntimeConstructorInfo)
{
return (InvokeFunc_RefArgs)InterpretedInvoke_Constructor;
}

Debug.Assert(_method is DynamicMethod);
return (InvokeFunc_RefArgs)InterpretedInvoke_DynamicMethod;
}

private unsafe object? InterpretedInvoke_Constructor(object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: obj is null);
private unsafe object? InterpretedInvoke_Method(IntPtr _, object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((RuntimeMethodInfo)_method).Signature, isConstructor: false);

private unsafe object? InterpretedInvoke_Constructor(IntPtr _, object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((RuntimeConstructorInfo)_method).Signature, isConstructor: obj is null);

private unsafe object? InterpretedInvoke_Method(object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: false);
private unsafe object? InterpretedInvoke_DynamicMethod(IntPtr _, object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((DynamicMethod)_method).Signature, isConstructor: false);
}
}
Loading
Loading