Skip to content
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

Remove Helper Method Frames (HMF) from Reflection #110627

Merged
merged 22 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fbf8378
Convert Signature.GetSignature() to QCall.
AaronRobinsonMSFT Dec 7, 2024
2ddcd69
Remove HMF from Signature.GetTypeParameterOffset().
AaronRobinsonMSFT Dec 7, 2024
5410c4d
Remove HMF from Signature.GetCallingConventionFromFunctionPointerAtOf…
AaronRobinsonMSFT Dec 7, 2024
ec18691
Convert Signature.GetCustomModifiersAtOffset() to QCall.
AaronRobinsonMSFT Dec 9, 2024
eb104fd
Convert RuntimeTypeHandle.GetDeclaringMethodForGenericParameter() to …
AaronRobinsonMSFT Dec 9, 2024
a392aef
Convert RuntimeTypeHandle.CanCastTo to slow/fast
AaronRobinsonMSFT Dec 9, 2024
d887c89
Remove HMF from RuntimeMethodHandle::GetMethodDef().
AaronRobinsonMSFT Dec 10, 2024
94cfb16
Convert RuntimeMethodHandle.GetStubIfNeeded to fast/slow
AaronRobinsonMSFT Dec 10, 2024
f9ff74a
Convert RuntimeTypeHandle.SatisfiesConstraints() to QCall.
AaronRobinsonMSFT Dec 11, 2024
fffc08e
Convert RuntimeMethodHandle.GetMethodBody() to QCall.
AaronRobinsonMSFT Dec 11, 2024
6708092
Fix build.
AaronRobinsonMSFT Dec 11, 2024
66627e3
Merge remote-tracking branch 'upstream/main' into hmf_remove09
AaronRobinsonMSFT Dec 11, 2024
e5c2882
Revert "Convert RuntimeMethodHandle.GetMethodBody() to QCall."
AaronRobinsonMSFT Dec 12, 2024
90ac98f
Update type definitions.
AaronRobinsonMSFT Dec 12, 2024
6b9bc4c
Revert "Revert "Convert RuntimeMethodHandle.GetMethodBody() to QCall.""
AaronRobinsonMSFT Dec 12, 2024
79a9454
Ensure the RuntimeMethodBody..ctor() isn't removed.
AaronRobinsonMSFT Dec 12, 2024
f39f202
Feedback 1
AaronRobinsonMSFT Dec 15, 2024
448b19e
Feedback 2
AaronRobinsonMSFT Dec 16, 2024
4317d9f
Feedback
AaronRobinsonMSFT Dec 16, 2024
908b7f4
Update RuntimeTypeHandle_SatisfiesConstraints signature.
AaronRobinsonMSFT Dec 16, 2024
0f5ac4f
Move more CanCastTo to managed.
AaronRobinsonMSFT Dec 16, 2024
f8c105c
Feedback
AaronRobinsonMSFT Dec 17, 2024
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 @@ -1101,18 +1101,25 @@ public static TypeHandle TypeHandleOf<T>()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanCastTo(TypeHandle destTH)
{
if (m_asTAddr == destTH.m_asTAddr)
return true;

if (!IsTypeDesc && destTH.IsTypeDesc)
return false;
return TryCanCastTo(this, destTH) switch
{
CastResult.CanCast => true,
CastResult.CannotCast => false,
_ => CanCastTo_NoCacheLookup(m_asTAddr, destTH.m_asTAddr)
};
}

CastResult result = CastCache.TryGet(CastHelpers.s_table!, (nuint)m_asTAddr, (nuint)destTH.m_asTAddr);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static CastResult TryCanCastTo(TypeHandle srcTH, TypeHandle destTH)
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
{
// See TypeHandle::CanCastToCached() for duplicate quick checks.
if (srcTH.m_asTAddr == destTH.m_asTAddr)
return CastResult.CanCast;

if (result != CastResult.MaybeCast)
return result == CastResult.CanCast;
if (!srcTH.IsTypeDesc && destTH.IsTypeDesc)
return CastResult.CannotCast;

return CanCastTo_NoCacheLookup(m_asTAddr, destTH.m_asTAddr);
return CastCache.TryGet(CastHelpers.s_table!, (nuint)srcTH.m_asTAddr, (nuint)destTH.m_asTAddr);
}

public int GetCorElementType() => GetCorElementType(m_asTAddr);
Expand Down
258 changes: 193 additions & 65 deletions src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2011,29 +2011,23 @@ private static RuntimePropertyInfo GetPropertyInfo(RuntimeType reflectedType, in

internal static void ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception? e)
{
RuntimeType[]? typeContext = null;
RuntimeType[]? methodContext = null;
RuntimeType? typeContext;
RuntimeMethodInfo? methodContext = null;
RuntimeType[] genericParameters;

if (definition is Type)
{
RuntimeType genericTypeDefinition = (RuntimeType)definition;
genericParameters = genericTypeDefinition.GetGenericArgumentsInternal();
typeContext = genericArguments;
typeContext = (RuntimeType)definition;
genericParameters = typeContext.GetGenericArgumentsInternal();
}
else
{
RuntimeMethodInfo genericMethodDefinition = (RuntimeMethodInfo)definition;
genericParameters = genericMethodDefinition.GetGenericArgumentsInternal();
methodContext = genericArguments;

RuntimeType? declaringType = (RuntimeType?)genericMethodDefinition.DeclaringType;
if (declaringType != null)
{
typeContext = declaringType.TypeHandle.GetInstantiationInternal();
}
methodContext = (RuntimeMethodInfo)definition;
typeContext = (RuntimeType?)methodContext.DeclaringType;
genericParameters = methodContext.GetGenericArgumentsInternal();
}

Debug.Assert(genericArguments.Length == genericParameters.Length);
for (int i = 0; i < genericArguments.Length; i++)
{
Type genericArgument = genericArguments[i];
Expand Down Expand Up @@ -3276,8 +3270,7 @@ public override MethodBase? DeclaringMethod
if (!IsGenericParameter)
throw new InvalidOperationException(SR.Arg_NotGenericParameter);

IRuntimeMethodInfo declaringMethod = RuntimeTypeHandle.GetDeclaringMethod(this);

IRuntimeMethodInfo? declaringMethod = RuntimeTypeHandle.GetDeclaringMethodForGenericParameter(this);
if (declaringMethod == null)
return null;

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/assemblynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetEntryPoint(QCall::AssemblyHandle pAs
if (pMeth != NULL)
{
GCX_COOP();
retMethod.Set(pMeth->GetStubMethodInfo());
retMethod.Set(pMeth->AllocateStubMethodInfo());
}

END_QCALL;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/comdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ extern "C" void QCALLTYPE Delegate_FindMethodHandle(QCall::ObjectHandleOnStack d

MethodDesc* pMD = COMDelegate::GetMethodDesc(d.Get());
pMD = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMD, TypeHandle(pMD->GetMethodTable()), pMD->GetMethodInstantiation());
retMethodInfo.Set(pMD->GetStubMethodInfo());
retMethodInfo.Set(pMD->AllocateStubMethodInfo());

END_QCALL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ extern "C" void QCALLTYPE ExceptionNative_GetMethodFromStackTrace(QCall::ObjectH
// The managed stack trace classes always return typical method definition,
// so we don't need to bother providing exact instantiation.
MethodDesc* pMDTypical = pMD->LoadTypicalMethodDefinition();
retMethodInfo.Set(pMDTypical->GetStubMethodInfo());
retMethodInfo.Set(pMDTypical->AllocateStubMethodInfo());
_ASSERTE(pMDTypical->IsRuntimeMethodHandle());

END_QCALL;
Expand Down
21 changes: 13 additions & 8 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ DEFINE_METHOD(ASSEMBLY, CTOR, .ctor,
DEFINE_CLASS(ASYNCCALLBACK, System, AsyncCallback)
DEFINE_CLASS(ATTRIBUTE, System, Attribute)

DEFINE_CLASS_U(System, Signature, SignatureNative)

DEFINE_CLASS(BINDER, Reflection, Binder)

Expand Down Expand Up @@ -182,7 +183,7 @@ DEFINE_METHOD(COM_OBJECT, RELEASE_ALL_DATA, ReleaseAllData,
DEFINE_METHOD(COM_OBJECT, GET_EVENT_PROVIDER, GetEventProvider, IM_Class_RetObj)
#ifdef FOR_ILLINK
DEFINE_METHOD(COM_OBJECT, CTOR, .ctor, IM_RetVoid)
#endif
#endif // FOR_ILLINK

DEFINE_CLASS(LICENSE_INTEROP_PROXY, InternalInteropServices, LicenseInteropProxy)
DEFINE_METHOD(LICENSE_INTEROP_PROXY, CREATE, Create, SM_RetObj)
Expand Down Expand Up @@ -358,9 +359,9 @@ DEFINE_FIELD(RT_FIELD_INFO, HANDLE, m_fieldHandle)
DEFINE_CLASS_U(System, RuntimeFieldInfoStub, ReflectFieldObject)
DEFINE_FIELD_U(m_fieldHandle, ReflectFieldObject, m_pFD)
DEFINE_CLASS(STUBFIELDINFO, System, RuntimeFieldInfoStub)
#if FOR_ILLINK
#ifdef FOR_ILLINK
DEFINE_METHOD(STUBFIELDINFO, CTOR, .ctor, IM_RetVoid)
#endif
#endif // FOR_ILLINK

DEFINE_CLASS(FIELD, Reflection, RuntimeFieldInfo)

Expand Down Expand Up @@ -497,18 +498,18 @@ DEFINE_FIELD_U(_handlerLength, RuntimeExceptionHandlingClause, _h
DEFINE_FIELD_U(_catchMetadataToken, RuntimeExceptionHandlingClause, _catchToken)
DEFINE_FIELD_U(_filterOffset, RuntimeExceptionHandlingClause, _filterOffset)
DEFINE_CLASS(RUNTIME_EH_CLAUSE, Reflection, RuntimeExceptionHandlingClause)
#if FOR_ILLINK
#ifdef FOR_ILLINK
DEFINE_METHOD(RUNTIME_EH_CLAUSE, CTOR, .ctor, IM_RetVoid)
#endif
#endif // FOR_ILLINK

DEFINE_CLASS_U(Reflection, RuntimeLocalVariableInfo, RuntimeLocalVariableInfo)
DEFINE_FIELD_U(_type, RuntimeLocalVariableInfo, _type)
DEFINE_FIELD_U(_localIndex, RuntimeLocalVariableInfo, _localIndex)
DEFINE_FIELD_U(_isPinned, RuntimeLocalVariableInfo, _isPinned)
DEFINE_CLASS(RUNTIME_LOCAL_VARIABLE_INFO, Reflection, RuntimeLocalVariableInfo)
#if FOR_ILLINK
#ifdef FOR_ILLINK
DEFINE_METHOD(RUNTIME_LOCAL_VARIABLE_INFO, CTOR, .ctor, IM_RetVoid)
#endif
#endif // FOR_ILLINK

DEFINE_CLASS_U(Reflection, RuntimeMethodBody, RuntimeMethodBody)
DEFINE_FIELD_U(_IL, RuntimeMethodBody, _IL)
Expand All @@ -518,7 +519,11 @@ DEFINE_FIELD_U(_methodBase, RuntimeMethodBody, _methodBase
DEFINE_FIELD_U(_localSignatureMetadataToken, RuntimeMethodBody, _localVarSigToken)
DEFINE_FIELD_U(_maxStackSize, RuntimeMethodBody, _maxStackSize)
DEFINE_FIELD_U(_initLocals, RuntimeMethodBody, _initLocals)
DEFINE_CLASS(RUNTIME_METHOD_BODY, Reflection, RuntimeMethodBody)

DEFINE_CLASS(RUNTIME_METHOD_BODY, Reflection, RuntimeMethodBody)
#ifdef FOR_ILLINK
DEFINE_METHOD(RUNTIME_METHOD_BODY, CTOR, .ctor, IM_RetVoid)
#endif // FOR_ILLINK

DEFINE_CLASS(METHOD_INFO, Reflection, MethodInfo)

Expand Down
12 changes: 3 additions & 9 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ FCFuncStart(gExceptionFuncs)
FCFuncEnd()

FCFuncStart(gCOMTypeHandleFuncs)
FCFuncElement("GetDeclaringMethod", RuntimeTypeHandle::GetDeclaringMethod)
FCFuncElement("GetFirstIntroducedMethod", RuntimeTypeHandle::GetFirstIntroducedMethod)
FCFuncElement("GetNextIntroducedMethod", RuntimeTypeHandle::GetNextIntroducedMethod)
FCFuncElement("GetAssemblyIfExists", RuntimeTypeHandle::GetAssemblyIfExists)
Expand All @@ -93,11 +92,9 @@ FCFuncStart(gCOMTypeHandleFuncs)
FCFuncElement("GetUtf8NameInternal", RuntimeTypeHandle::GetUtf8Name)
FCFuncElement("GetAttributes", RuntimeTypeHandle::GetAttributes)
FCFuncElement("GetNumVirtuals", RuntimeTypeHandle::GetNumVirtuals)
FCFuncElement("CanCastTo", RuntimeTypeHandle::CanCastTo)
FCFuncElement("GetGenericVariableIndex", RuntimeTypeHandle::GetGenericVariableIndex)
FCFuncElement("IsGenericVariable", RuntimeTypeHandle::IsGenericVariable)
FCFuncElement("ContainsGenericVariables", RuntimeTypeHandle::ContainsGenericVariables)
FCFuncElement("SatisfiesConstraints", RuntimeTypeHandle::SatisfiesConstraints)
FCFuncElement("IsUnmanagedFunctionPointer", RuntimeTypeHandle::IsUnmanagedFunctionPointer)
FCFuncElement("CompareCanonicalHandles", RuntimeTypeHandle::CompareCanonicalHandles)
FCFuncElement("GetRuntimeTypeFromHandleIfExists", RuntimeTypeHandle::GetRuntimeTypeFromHandleIfExists)
Expand Down Expand Up @@ -132,11 +129,9 @@ FCFuncStart(gMetaDataImport)
FCFuncEnd()

FCFuncStart(gSignatureNative)
FCFuncElement("GetSignature", SignatureNative::GetSignature)
FCFuncElement("GetParameterOffsetInternal", SignatureNative::GetParameterOffsetInternal)
FCFuncElement("GetTypeParameterOffset", SignatureNative::GetTypeParameterOffset)
FCFuncElement("GetCustomModifiersAtOffset", SignatureNative::GetCustomModifiersAtOffset)
FCFuncElement("GetCallingConventionFromFunctionPointerAtOffset", SignatureNative::GetCallingConventionFromFunctionPointerAtOffset)
FCFuncElement("GetTypeParameterOffsetInternal", SignatureNative::GetTypeParameterOffsetInternal)
FCFuncElement("GetCallingConventionFromFunctionPointerAtOffsetInternal", SignatureNative::GetCallingConventionFromFunctionPointerAtOffsetInternal)
FCFuncEnd()

FCFuncStart(gRuntimeMethodHandle)
Expand All @@ -150,10 +145,9 @@ FCFuncStart(gRuntimeMethodHandle)
FCFuncElement("IsGenericMethodDefinition", RuntimeMethodHandle::IsGenericMethodDefinition)
FCFuncElement("GetGenericParameterCount", RuntimeMethodHandle::GetGenericParameterCount)
FCFuncElement("IsTypicalMethodDefinition", RuntimeMethodHandle::IsTypicalMethodDefinition)
FCFuncElement("GetStubIfNeeded", RuntimeMethodHandle::GetStubIfNeeded)
FCFuncElement("GetStubIfNeededInternal", RuntimeMethodHandle::GetStubIfNeededInternal)
FCFuncElement("GetMethodFromCanonical", RuntimeMethodHandle::GetMethodFromCanonical)
FCFuncElement("IsDynamicMethod", RuntimeMethodHandle::IsDynamicMethod)
FCFuncElement("GetMethodBody", RuntimeMethodHandle::GetMethodBody)
FCFuncElement("IsConstructor", RuntimeMethodHandle::IsConstructor)
FCFuncElement("GetResolver", RuntimeMethodHandle::GetResolver)
FCFuncElement("GetLoaderAllocatorInternal", RuntimeMethodHandle::GetLoaderAllocatorInternal)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/interoplibinterface_objc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ void* ObjCMarshalNative::GetPropagatingExceptionCallback(
if (CallAvailableUnhandledExceptionPropagation())
{
gc.throwableRef = ObjectFromHandle(throwable);
gc.methodRef = method->GetStubMethodInfo();
gc.methodRef = method->AllocateStubMethodInfo();

callback = CallInvokeUnhandledExceptionPropagation(
&gc.throwableRef,
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6536,7 +6536,7 @@ void Interpreter::LdToken()
if (tok.hMethod != NULL)
{
MethodDesc* pMethod = (MethodDesc*)tok.hMethod;
Object* objPtr = OBJECTREFToObject((OBJECTREF)pMethod->GetStubMethodInfo());
Object* objPtr = OBJECTREFToObject((OBJECTREF)pMethod->AllocateStubMethodInfo());
OpStackSet<Object*>(m_curStackHt, objPtr);
}
else if (tok.hField != NULL)
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ HCIMPL1(Object*, JIT_GetRuntimeMethodStub, CORINFO_METHOD_HANDLE method)
HELPER_METHOD_FRAME_BEGIN_RET_0(); // Set up a frame

MethodDesc *pMethod = (MethodDesc *)method;
stubRuntimeMethod = (OBJECTREF)pMethod->GetStubMethodInfo();
stubRuntimeMethod = (OBJECTREF)pMethod->AllocateStubMethodInfo();

HELPER_METHOD_FRAME_END();

Expand Down Expand Up @@ -4049,7 +4049,7 @@ bool IndirectionAllowedForJitHelper(CorInfoHelpFunc ftnNum)
{
return false;
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3724,7 +3724,7 @@ PTR_LoaderAllocator MethodDesc::GetLoaderAllocator()
}

#if !defined(DACCESS_COMPILE)
REFLECTMETHODREF MethodDesc::GetStubMethodInfo()
REFLECTMETHODREF MethodDesc::AllocateStubMethodInfo()
{
CONTRACTL
{
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/method.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,8 @@ class MethodDesc
public:
MethodDesc *GetInterfaceMD();

// StubMethodInfo for use in creating RuntimeMethodHandles
REFLECTMETHODREF GetStubMethodInfo();
// StubMethodInfo for use in creating RuntimeMethodHandles
REFLECTMETHODREF AllocateStubMethodInfo();

PrecodeType GetPrecodeType();

Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,14 @@ static const Entry s_QCall[] =
DllImportEntry(RuntimeTypeHandle_GetFields)
DllImportEntry(RuntimeTypeHandle_VerifyInterfaceIsImplemented)
DllImportEntry(RuntimeTypeHandle_GetInterfaceMethodImplementation)
DllImportEntry(RuntimeTypeHandle_GetDeclaringMethodForGenericParameter)
DllImportEntry(RuntimeTypeHandle_GetDeclaringTypeHandleForGenericVariable)
DllImportEntry(RuntimeTypeHandle_GetDeclaringTypeHandle)
DllImportEntry(RuntimeTypeHandle_IsVisible)
DllImportEntry(RuntimeTypeHandle_ConstructName)
DllImportEntry(RuntimeTypeHandle_GetInterfaces)
DllImportEntry(RuntimeTypeHandle_CanCastToSlow)
DllImportEntry(RuntimeTypeHandle_SatisfiesConstraints)
DllImportEntry(RuntimeTypeHandle_GetInstantiation)
DllImportEntry(RuntimeTypeHandle_Instantiate)
DllImportEntry(RuntimeTypeHandle_GetGenericTypeDefinition)
Expand All @@ -158,6 +161,8 @@ static const Entry s_QCall[] =
DllImportEntry(RuntimeMethodHandle_StripMethodInstantiation)
DllImportEntry(RuntimeMethodHandle_IsCAVisibleFromDecoratedType)
DllImportEntry(RuntimeMethodHandle_Destroy)
DllImportEntry(RuntimeMethodHandle_GetStubIfNeededSlow)
DllImportEntry(RuntimeMethodHandle_GetMethodBody)
DllImportEntry(RuntimeModule_GetScopeName)
DllImportEntry(RuntimeModule_GetFullyQualifiedName)
DllImportEntry(RuntimeModule_GetTypes)
Expand Down Expand Up @@ -187,7 +192,9 @@ static const Entry s_QCall[] =
DllImportEntry(ModuleHandle_GetPEKind)
DllImportEntry(ModuleHandle_GetDynamicMethod)
DllImportEntry(AssemblyHandle_GetManifestModuleSlow)
DllImportEntry(Signature_Init)
DllImportEntry(Signature_AreEqual)
DllImportEntry(Signature_GetCustomModifiersAtOffset)
DllImportEntry(TypeBuilder_DefineGenericParam)
DllImportEntry(TypeBuilder_DefineType)
DllImportEntry(TypeBuilder_SetParentType)
Expand Down
Loading
Loading