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

Move GetMethodTable to JIT #105098

Merged
merged 4 commits into from
Aug 19, 2024
Merged
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 @@ -436,16 +436,8 @@ internal static unsafe bool ObjectHasComponentSize(object obj)
//
// GC.KeepAlive(o);
//
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Intrinsic]
internal static unsafe MethodTable* GetMethodTable(object obj)
{
// The body of this function will be replaced by the EE with unsafe code
// See getILIntrinsicImplementationForRuntimeHelpers for how this happens.

return (MethodTable*)Unsafe.Add(ref Unsafe.As<byte, IntPtr>(ref obj.GetRawData()), -1);
}

internal static unsafe MethodTable* GetMethodTable(object obj) => GetMethodTable(obj);

[LibraryImport(QCall, EntryPoint = "MethodTable_AreTypesEquivalent")]
[return: MarshalAs(UnmanagedType.Bool)]
Expand Down
10 changes: 10 additions & 0 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3561,6 +3561,12 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd,
break;
}

case NI_System_Runtime_CompilerServices_RuntimeHelpers_GetMethodTable:
{
retNode = gtNewMethodTableLookup(impPopStack().val);
break;
}

case NI_System_Runtime_InteropService_MemoryMarshal_GetArrayDataReference:
{
assert(sig->numArgs == 1);
Expand Down Expand Up @@ -10410,6 +10416,10 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
result =
NI_System_Runtime_CompilerServices_RuntimeHelpers_IsReferenceOrContainsReferences;
}
else if (strcmp(methodName, "GetMethodTable") == 0)
{
result = NI_System_Runtime_CompilerServices_RuntimeHelpers_GetMethodTable;
}
}
else if (strcmp(className, "Unsafe") == 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/namedintrinsiclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum NamedIntrinsic : unsigned short
NI_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray,
NI_System_Runtime_CompilerServices_RuntimeHelpers_IsKnownConstant,
NI_System_Runtime_CompilerServices_RuntimeHelpers_IsReferenceOrContainsReferences,
NI_System_Runtime_CompilerServices_RuntimeHelpers_GetMethodTable,

NI_System_Runtime_InteropService_MemoryMarshal_GetArrayDataReference,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ internal static unsafe ushort GetElementSize(this Array array)
return array.GetMethodTable()->ComponentSize;
}

internal static unsafe MethodTable* GetMethodTable(this object obj)
=> obj.m_pEEType;
[Intrinsic]
internal static unsafe MethodTable* GetMethodTable(this object obj) => obj.GetMethodTable();

internal static unsafe ref MethodTable* GetMethodTableRef(this object obj)
=> ref obj.m_pEEType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ public static MethodIL EmitIL(MethodDesc method)
Debug.Assert(((MetadataType)method.OwningType).Name == "RuntimeHelpers");
string methodName = method.Name;

if (methodName == "GetMethodTable")
{
ILEmitter emit = new ILEmitter();
ILCodeStream codeStream = emit.NewCodeStream();
codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldflda, emit.NewToken(method.Context.SystemModule.GetKnownType("System.Runtime.CompilerServices", "RawData").GetField("Data")));
codeStream.EmitLdc(-method.Context.Target.PointerSize);
codeStream.Emit(ILOpcode.add);
codeStream.Emit(ILOpcode.ldind_i);
codeStream.Emit(ILOpcode.ret);
return emit.Link(method);
}

// All the methods handled below are per-instantiation generic methods
if (method.Instantiation.Length != 1 || method.IsTypicalMethodDefinition)
return null;
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,6 @@ DEFINE_METHOD(RTFIELD, GET_FIELDHANDLE, GetFieldHandle,

DEFINE_CLASS(RUNTIME_HELPERS, CompilerServices, RuntimeHelpers)
DEFINE_METHOD(RUNTIME_HELPERS, IS_BITWISE_EQUATABLE, IsBitwiseEquatable, NoSig)
DEFINE_METHOD(RUNTIME_HELPERS, GET_METHOD_TABLE, GetMethodTable, NoSig)
DEFINE_METHOD(RUNTIME_HELPERS, GET_RAW_DATA, GetRawData, NoSig)
DEFINE_METHOD(RUNTIME_HELPERS, GET_UNINITIALIZED_OBJECT, GetUninitializedObject, SM_Type_RetObj)
DEFINE_METHOD(RUNTIME_HELPERS, ENUM_EQUALS, EnumEquals, NoSig)
Expand Down
37 changes: 0 additions & 37 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7332,43 +7332,6 @@ bool getILIntrinsicImplementationForRuntimeHelpers(MethodDesc * ftn,
return true;
}

if (tk == CoreLibBinder::GetMethod(METHOD__RUNTIME_HELPERS__GET_METHOD_TABLE)->GetMemberDef())
{
mdToken tokRawData = CoreLibBinder::GetField(FIELD__RAW_DATA__DATA)->GetMemberDef();

// In the CLR, an object is laid out as follows.
// [ object_header || MethodTable* (64-bit pointer) || instance_data ]
// ^ ^-- ref <theObj>.firstField points here
// `-- <theObj> reference (type O) points here
//
// So essentially what we want to do is to turn an object reference (type O) into a
// native int&, then dereference it to get the MethodTable*. (Essentially, an object
// reference is a MethodTable**.) Per ECMA-335, Sec. III.1.5, we can add
// (but not subtract) a & and an int32 to produce a &. So we'll get a reference to
// <theObj>.firstField (type &), then back up one pointer length to get a value of
// essentially type (MethodTable*)&. Both of these are legal GC-trackable references
// to <theObj>, regardless of <theObj>'s actual length.

static BYTE ilcode[] = { CEE_LDARG_0, // stack contains [ O ] = <theObj>
CEE_LDFLDA,0,0,0,0, // stack contains [ & ] = ref <theObj>.firstField
CEE_LDC_I4_S,(BYTE)(-TARGET_POINTER_SIZE), // stack contains [ &, int32 ] = -IntPtr.Size
CEE_ADD, // stack contains [ & ] = ref <theObj>.methodTablePtr
CEE_LDIND_I, // stack contains [ native int ] = <theObj>.methodTablePtr
CEE_RET };

ilcode[2] = (BYTE)(tokRawData);
ilcode[3] = (BYTE)(tokRawData >> 8);
ilcode[4] = (BYTE)(tokRawData >> 16);
ilcode[5] = (BYTE)(tokRawData >> 24);

methInfo->ILCode = const_cast<BYTE*>(ilcode);
methInfo->ILCodeSize = sizeof(ilcode);
methInfo->maxStack = 2;
methInfo->EHcount = 0;
methInfo->options = (CorInfoOptions)0;
return true;
}

if (tk == CoreLibBinder::GetMethod(METHOD__RUNTIME_HELPERS__ENUM_EQUALS)->GetMemberDef())
{
// Normally we would follow the above pattern and unconditionally replace the IL,
Expand Down
Loading