Skip to content

Commit 2d81cbb

Browse files
davidwrightonam11teo-tsirpanis
authored
Remove 2 more casting helper method frames (#110064)
* Remove 2 more casting helper method frames * Update src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs Co-authored-by: Adeel Mujahid <[email protected]> * Update src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs Co-authored-by: Adeel Mujahid <[email protected]> * Update src/coreclr/vm/jitinterface.h Co-authored-by: Theodore Tsirpanis <[email protected]> * More code updates as suggested by am11 --------- Co-authored-by: Adeel Mujahid <[email protected]> Co-authored-by: Theodore Tsirpanis <[email protected]>
1 parent 43d1838 commit 2d81cbb

File tree

6 files changed

+34
-52
lines changed

6 files changed

+34
-52
lines changed

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs

+23-9
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,26 @@ internal static void ThrowInvalidCastException(object fromType, void* toTypeHnd)
2424
throw null!; // Provide hint to the inliner that this method does not return
2525
}
2626

27-
[MethodImpl(MethodImplOptions.InternalCall)]
28-
private static extern object IsInstanceOfAny_NoCacheLookup(void* toTypeHnd, object obj);
27+
[LibraryImport(RuntimeHelpers.QCall)]
28+
[return: MarshalAs(UnmanagedType.Bool)]
29+
private static partial bool IsInstanceOf_NoCacheLookup(void *toTypeHnd, [MarshalAs(UnmanagedType.Bool)] bool throwCastException, ObjectHandleOnStack obj);
2930

30-
[MethodImpl(MethodImplOptions.InternalCall)]
31-
private static extern object ChkCastAny_NoCacheLookup(void* toTypeHnd, object obj);
31+
[MethodImpl(MethodImplOptions.NoInlining)]
32+
private static object? IsInstanceOfAny_NoCacheLookup(void* toTypeHnd, object obj)
33+
{
34+
if (IsInstanceOf_NoCacheLookup(toTypeHnd, false, ObjectHandleOnStack.Create(ref obj)))
35+
{
36+
return obj;
37+
}
38+
return null;
39+
}
40+
41+
[MethodImpl(MethodImplOptions.NoInlining)]
42+
private static object ChkCastAny_NoCacheLookup(void* toTypeHnd, object obj)
43+
{
44+
IsInstanceOf_NoCacheLookup(toTypeHnd, true, ObjectHandleOnStack.Create(ref obj));
45+
return obj;
46+
}
3247

3348
[MethodImpl(MethodImplOptions.InternalCall)]
3449
private static extern void WriteBarrier(ref object? dst, object? obj);
@@ -464,13 +479,13 @@ private static void StelemRef_Helper_NoCacheLookup(ref object? element, void* el
464479
{
465480
Debug.Assert(obj != null);
466481

467-
obj = IsInstanceOfAny_NoCacheLookup(elementType, obj);
468-
if (obj == null)
482+
object? obj2 = IsInstanceOfAny_NoCacheLookup(elementType, obj);
483+
if (obj2 == null)
469484
{
470485
ThrowArrayMismatchException();
471486
}
472487

473-
WriteBarrier(ref element, obj);
488+
WriteBarrier(ref element, obj2);
474489
}
475490

476491
[DebuggerHidden]
@@ -496,8 +511,7 @@ private static unsafe void ArrayTypeCheck_Helper(object obj, void* elementType)
496511
{
497512
Debug.Assert(obj != null);
498513

499-
obj = IsInstanceOfAny_NoCacheLookup(elementType, obj);
500-
if (obj == null)
514+
if (IsInstanceOfAny_NoCacheLookup(elementType, obj) == null)
501515
{
502516
ThrowArrayMismatchException();
503517
}

src/coreclr/vm/JitQCallHelpers.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ extern "C" void QCALLTYPE InitClassHelper(MethodTable* pMT);
2424
extern "C" void QCALLTYPE ThrowInvalidCastException(CORINFO_CLASS_HANDLE pTargetType, CORINFO_CLASS_HANDLE pSourceType);
2525
extern "C" void QCALLTYPE GetThreadStaticsByMethodTable(QCall::ByteRefOnStack refHandle, MethodTable* pMT, bool gcStatic);
2626
extern "C" void QCALLTYPE GetThreadStaticsByIndex(QCall::ByteRefOnStack refHandle, uint32_t staticBlockIndex, bool gcStatic);
27+
extern "C" BOOL QCALLTYPE IsInstanceOf_NoCacheLookup(CORINFO_CLASS_HANDLE type, BOOL throwCastException, QCall::ObjectHandleOnStack objOnStack);
2728

2829
#endif //_JITQCALLHELPERS_H

src/coreclr/vm/ecalllist.h

-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ FCFuncStart(gThreadPoolFuncs)
256256
FCFuncEnd()
257257

258258
FCFuncStart(gCastHelpers)
259-
FCFuncElement("IsInstanceOfAny_NoCacheLookup", ::IsInstanceOfAny_NoCacheLookup)
260-
FCFuncElement("ChkCastAny_NoCacheLookup", ::ChkCastAny_NoCacheLookup)
261259
FCFuncElement("WriteBarrier", ::WriteBarrier_Helper)
262260
FCFuncEnd()
263261

src/coreclr/vm/jithelpers.cpp

+8-37
Original file line numberDiff line numberDiff line change
@@ -781,51 +781,22 @@ BOOL ObjIsInstanceOf(Object* pObject, TypeHandle toTypeHnd, BOOL throwCastExcept
781781
return ObjIsInstanceOfCore(pObject, toTypeHnd, throwCastException);
782782
}
783783

784-
HCIMPL2(Object*, ChkCastAny_NoCacheLookup, CORINFO_CLASS_HANDLE type, Object* obj)
784+
extern "C" BOOL QCALLTYPE IsInstanceOf_NoCacheLookup(CORINFO_CLASS_HANDLE type, BOOL throwCastException, QCall::ObjectHandleOnStack objOnStack)
785785
{
786-
FCALL_CONTRACT;
787-
788-
// This case should be handled by frameless helper
789-
_ASSERTE(obj != NULL);
790-
791-
OBJECTREF oref = ObjectToOBJECTREF(obj);
792-
VALIDATEOBJECTREF(oref);
793-
794-
TypeHandle clsHnd(type);
795-
796-
HELPER_METHOD_FRAME_BEGIN_RET_1(oref);
797-
if (!ObjIsInstanceOfCore(OBJECTREFToObject(oref), clsHnd, TRUE))
798-
{
799-
UNREACHABLE(); //ObjIsInstanceOf will throw if cast can't be done
800-
}
801-
HELPER_METHOD_POLL();
802-
HELPER_METHOD_FRAME_END();
803-
804-
return OBJECTREFToObject(oref);
805-
}
806-
HCIMPLEND
807-
808-
HCIMPL2(Object*, IsInstanceOfAny_NoCacheLookup, CORINFO_CLASS_HANDLE type, Object* obj)
809-
{
810-
FCALL_CONTRACT;
786+
QCALL_CONTRACT;
787+
BOOL result = FALSE;
811788

812-
// This case should be handled by frameless helper
813-
_ASSERTE(obj != NULL);
789+
BEGIN_QCALL;
814790

815-
OBJECTREF oref = ObjectToOBJECTREF(obj);
816-
VALIDATEOBJECTREF(oref);
791+
GCX_COOP();
817792

818793
TypeHandle clsHnd(type);
794+
result = ObjIsInstanceOfCore(OBJECTREFToObject(objOnStack.Get()), clsHnd, throwCastException);
819795

820-
HELPER_METHOD_FRAME_BEGIN_RET_1(oref);
821-
if (!ObjIsInstanceOfCore(OBJECTREFToObject(oref), clsHnd))
822-
oref = NULL;
823-
HELPER_METHOD_POLL();
824-
HELPER_METHOD_FRAME_END();
796+
END_QCALL;
825797

826-
return OBJECTREFToObject(oref);
798+
return result;
827799
}
828-
HCIMPLEND
829800

830801
//========================================================================
831802
//

src/coreclr/vm/jitinterface.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ extern "C" FCDECL2(VOID, JIT_CheckedWriteBarrier, Object **dst, Object *ref);
230230
extern "C" FCDECL2(VOID, JIT_WriteBarrier, Object **dst, Object *ref);
231231
extern "C" FCDECL2(VOID, JIT_WriteBarrierEnsureNonHeapTarget, Object **dst, Object *ref);
232232

233-
extern "C" FCDECL2(Object*, ChkCastAny_NoCacheLookup, CORINFO_CLASS_HANDLE type, Object* obj);
234-
extern "C" FCDECL2(Object*, IsInstanceOfAny_NoCacheLookup, CORINFO_CLASS_HANDLE type, Object* obj);
235-
236-
// ARM64 JIT_WriteBarrier uses speciall ABI and thus is not callable directly
233+
// ARM64 JIT_WriteBarrier uses special ABI and thus is not callable directly
237234
// Copied write barriers must be called at a different location
238235
extern "C" FCDECL2(VOID, JIT_WriteBarrier_Callable, Object **dst, Object *ref);
239236

src/coreclr/vm/qcallentrypoints.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ static const Entry s_QCall[] =
514514
DllImportEntry(GetThreadStaticsByIndex)
515515
DllImportEntry(GenericHandleWorker)
516516
DllImportEntry(ThrowInvalidCastException)
517+
DllImportEntry(IsInstanceOf_NoCacheLookup)
517518
};
518519

519520
const void* QCallResolveDllImport(const char* name)

0 commit comments

Comments
 (0)