Skip to content

Commit

Permalink
Cleanup some code under FEATURE_STUBS_AS_IL (#104731)
Browse files Browse the repository at this point in the history
* Use direct SinglecastDelegateInvokeStub on windows x86

* Cleanup ifdefs

* EmitDebugBreak is unused

* Use IL stub for Delegate.Invoke

* Remove SinglecastDelegateInvokeStub in asm

* Fix method name and argument count

* Code quality

* Delete JIT_InternalThrow and CorInfoException

* Apply the same codegen to ilc

* Add KeepAlive

* Emit recursive call instead

* Add tests for indirect delegate invocation

* Use public reflection instead.

* Use ldvirtftn to bypass this check

* Use winner from CompareExchange

* Return Stub from GetInvokeMethodStub

* Delete NEWSTUB_FL_MULTICAST and dependents

* Handle new stub in ILStubManager

---------

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
huoyaoyuan and jkotas authored Sep 19, 2024
1 parent 51bd2b9 commit cc17a46
Show file tree
Hide file tree
Showing 44 changed files with 198 additions and 511 deletions.
16 changes: 0 additions & 16 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -826,22 +826,6 @@ enum CORINFO_EH_CLAUSE_FLAGS
CORINFO_EH_CLAUSE_SAMETRY = 0x0010, // This clause covers same try block as the previous one
};

// This enumeration is passed to InternalThrow
enum CorInfoException
{
CORINFO_NullReferenceException,
CORINFO_DivideByZeroException,
CORINFO_InvalidCastException,
CORINFO_IndexOutOfRangeException,
CORINFO_OverflowException,
CORINFO_SynchronizationLockException,
CORINFO_ArrayTypeMismatchException,
CORINFO_RankException,
CORINFO_ArgumentNullException,
CORINFO_ArgumentException,
CORINFO_Exception_Count,
};

// These are used to detect array methods as NamedIntrinsic in JIT importer,
// which otherwise don't have a name.
enum class CorInfoArrayIntrinsic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ public static MethodIL EmitIL(MethodDesc method)
FieldDesc functionPointerField = delegateType.GetKnownField("_functionPointer");
ILCodeStream codeStream = emit.NewCodeStream();

// Store the function pointer into local variable to avoid unnecessary register usage by JIT
ILLocalVariable functionPointer = emit.NewLocal(context.GetWellKnownType(WellKnownType.IntPtr));

codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldfld, emit.NewToken(functionPointerField.InstantiateAsOpen()));
codeStream.EmitStLoc(functionPointer);

codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldfld, emit.NewToken(firstParameterField.InstantiateAsOpen()));
for (int i = 0; i < method.Signature.Length; i++)
{
codeStream.EmitLdArg(i + 1);
}
codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldfld, emit.NewToken(functionPointerField.InstantiateAsOpen()));
codeStream.EmitLdLoc(functionPointer);

MethodSignature signature = method.Signature;
if (method.OwningType.HasInstantiation)
Expand Down
21 changes: 0 additions & 21 deletions src/coreclr/vm/amd64/AsmHelpers.asm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
include AsmMacros.inc
include asmconstants.inc

extern JIT_InternalThrow:proc
extern NDirectImportWorker:proc
extern ThePreStub:proc
extern ProfileEnter:proc
Expand Down Expand Up @@ -565,26 +564,6 @@ NESTED_ENTRY ProfileTailcallNaked, _TEXT
ret
NESTED_END ProfileTailcallNaked, _TEXT


extern JIT_InternalThrowFromHelper:proc

LEAF_ENTRY SinglecastDelegateInvokeStub, _TEXT

test rcx, rcx
jz NullObject


mov rax, [rcx + OFFSETOF__DelegateObject___methodPtr]
mov rcx, [rcx + OFFSETOF__DelegateObject___target] ; replace "this" pointer

jmp rax

NullObject:
mov rcx, CORINFO_NullReferenceException_ASM
jmp JIT_InternalThrow

LEAF_END SinglecastDelegateInvokeStub, _TEXT

ifdef FEATURE_TIERED_COMPILATION

extern OnCallCountThresholdReached:proc
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/amd64/JitHelpers_Fast.asm
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ ifdef _DEBUG
extern JIT_WriteBarrier_Debug:proc
endif

extern JIT_InternalThrow:proc


; JIT_ByRefWriteBarrier has weird semantics, see usage in StubLinkerX86.cpp
;
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/amd64/JitHelpers_Slow.asm
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ extern g_pStringClass:QWORD
extern FramedAllocateString:proc
extern JIT_NewArr1:proc

extern JIT_InternalThrow:proc

ifdef _DEBUG
; Version for when we're sure to be in the GC, checks whether or not the card
; needs to be updated
Expand Down
18 changes: 0 additions & 18 deletions src/coreclr/vm/amd64/asmconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ ASMCONSTANTS_C_ASSERT(OFFSETOF__ThreadExceptionState__m_pCurrentTracker



#define OFFSETOF__DelegateObject___methodPtr 0x18
ASMCONSTANT_OFFSETOF_ASSERT(DelegateObject, _methodPtr);

#define OFFSETOF__DelegateObject___target 0x08
ASMCONSTANT_OFFSETOF_ASSERT(DelegateObject, _target);

#define OFFSETOF__MethodTable__m_dwFlags 0x00
ASMCONSTANTS_C_ASSERT(OFFSETOF__MethodTable__m_dwFlags
== offsetof(MethodTable, m_dwFlags));
Expand Down Expand Up @@ -198,18 +192,6 @@ ASMCONSTANTS_C_ASSERT(OFFSETOF__DynamicStaticsInfo__m_pNonGCStatics
ASMCONSTANTS_C_ASSERT(OFFSETOF__DynamicStaticsInfo__m_pGCStatics
== offsetof(DynamicStaticsInfo, m_pGCStatics));

#define CORINFO_NullReferenceException_ASM 0
ASMCONSTANTS_C_ASSERT( CORINFO_NullReferenceException_ASM
== CORINFO_NullReferenceException);

#define CORINFO_IndexOutOfRangeException_ASM 3
ASMCONSTANTS_C_ASSERT( CORINFO_IndexOutOfRangeException_ASM
== CORINFO_IndexOutOfRangeException);

#define CORINFO_ArgumentException_ASM 9
ASMCONSTANTS_C_ASSERT( CORINFO_ArgumentException_ASM
== CORINFO_ArgumentException);


// MachState offsets (AMD64\gmscpu.h)

Expand Down
4 changes: 0 additions & 4 deletions src/coreclr/vm/amd64/cgencpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ class Module;
struct VASigCookie;
class ComCallMethodDesc;

//
// functions implemented in AMD64 assembly
//
EXTERN_C void SinglecastDelegateInvokeStub();

#define COMMETHOD_PREPAD 16 // # extra bytes to allocate in addition to sizeof(ComCallMethodDesc)
#define COMMETHOD_CALL_PRESTUB_SIZE 6 // 32-bit indirect relative call
Expand Down
17 changes: 0 additions & 17 deletions src/coreclr/vm/amd64/unixasmhelpers.S
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,6 @@ NESTED_END OnHijackTripThread, _TEXT

#endif // FEATURE_HIJACK

LEAF_ENTRY SinglecastDelegateInvokeStub, _TEXT

test rdi, rdi
jz NullObject


mov rax, [rdi + OFFSETOF__DelegateObject___methodPtr]
mov rdi, [rdi + OFFSETOF__DelegateObject___target] // replace "this" pointer

jmp rax

NullObject:
mov rdi, CORINFO_NullReferenceException_ASM
jmp C_FUNC(JIT_InternalThrow)

LEAF_END SinglecastDelegateInvokeStub, _TEXT

#ifdef FEATURE_TIERED_COMPILATION

NESTED_ENTRY OnCallCountThresholdReachedStub, _TEXT, NoHandler
Expand Down
14 changes: 0 additions & 14 deletions src/coreclr/vm/arm/asmconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@

#define REDIRECTSTUB_SP_OFFSET_CONTEXT 0

#define CORINFO_NullReferenceException_ASM 0
ASMCONSTANTS_C_ASSERT( CORINFO_NullReferenceException_ASM
== CORINFO_NullReferenceException);

#define CORINFO_IndexOutOfRangeException_ASM 3
ASMCONSTANTS_C_ASSERT( CORINFO_IndexOutOfRangeException_ASM
== CORINFO_IndexOutOfRangeException);


// Offset of the array containing the address of captured registers in MachState
#define MachState__captureR4_R11 0x0
Expand All @@ -64,12 +56,6 @@ ASMCONSTANTS_C_ASSERT(LazyMachState_captureSp == offsetof(LazyMachState, capture
#define LazyMachState_captureIp (LazyMachState_captureSp+4)
ASMCONSTANTS_C_ASSERT(LazyMachState_captureIp == offsetof(LazyMachState, captureIp))

#define DelegateObject___methodPtr 0x0c
ASMCONSTANTS_C_ASSERT(DelegateObject___methodPtr == offsetof(DelegateObject, _methodPtr));

#define DelegateObject___target 0x04
ASMCONSTANTS_C_ASSERT(DelegateObject___target == offsetof(DelegateObject, _target));

#define MethodTable__m_BaseSize 0x04
ASMCONSTANTS_C_ASSERT(MethodTable__m_BaseSize == offsetof(MethodTable, m_BaseSize));

Expand Down
16 changes: 0 additions & 16 deletions src/coreclr/vm/arm/asmhelpers.S
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,6 @@ CallDescrWorkerInternalReturnAddressOffset:

LEAF_END LazyMachStateCaptureState, _TEXT

// void SinglecastDelegateInvokeStub(Delegate *pThis)
LEAF_ENTRY SinglecastDelegateInvokeStub, _TEXT
cmp r0, #0
beq LOCAL_LABEL(LNullThis)

ldr r12, [r0, #DelegateObject___methodPtr]
ldr r0, [r0, #DelegateObject___target]

bx r12

LOCAL_LABEL(LNullThis):
mov r0, #CORINFO_NullReferenceException_ASM
b C_FUNC(JIT_InternalThrow)

LEAF_END SinglecastDelegateInvokeStub, _TEXT

//
// r12 = UMEntryThunk*
//
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/arm/cgencpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,6 @@ class StubLinkerCPU : public StubLinker
VOID EmitComputedInstantiatingMethodStub(MethodDesc* pSharedMD, struct ShuffleEntry *pShuffleEntryArray, void* extraArg);
};

extern "C" void SinglecastDelegateInvokeStub();

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4359) // Prevent "warning C4359: 'UMEntryThunkCode': Alignment specifier is less than actual alignment (8), and will be ignored." in crossbitness scenario
Expand Down
15 changes: 0 additions & 15 deletions src/coreclr/vm/arm64/asmconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@ ASMCONSTANTS_C_ASSERT(CallDescrData__pTarget == offsetof(CallDescrD
ASMCONSTANTS_C_ASSERT(CallDescrData__pRetBuffArg == offsetof(CallDescrData, pRetBuffArg))
ASMCONSTANTS_C_ASSERT(CallDescrData__returnValue == offsetof(CallDescrData, returnValue))

#define CORINFO_NullReferenceException_ASM 0
ASMCONSTANTS_C_ASSERT( CORINFO_NullReferenceException_ASM
== CORINFO_NullReferenceException);


#define CORINFO_IndexOutOfRangeException_ASM 3
ASMCONSTANTS_C_ASSERT( CORINFO_IndexOutOfRangeException_ASM
== CORINFO_IndexOutOfRangeException);


// Offset of the array containing the address of captured registers in MachState
#define MachState__captureX19_X29 0x0
Expand Down Expand Up @@ -109,12 +100,6 @@ ASMCONSTANTS_C_ASSERT(LazyMachState_captureIp == offsetof(LazyMachState, capture
#define VASigCookie__pNDirectILStub 0x8
ASMCONSTANTS_C_ASSERT(VASigCookie__pNDirectILStub == offsetof(VASigCookie, pNDirectILStub))

#define DelegateObject___methodPtr 0x18
ASMCONSTANTS_C_ASSERT(DelegateObject___methodPtr == offsetof(DelegateObject, _methodPtr));

#define DelegateObject___target 0x08
ASMCONSTANTS_C_ASSERT(DelegateObject___target == offsetof(DelegateObject, _target));

#define SIZEOF__GSCookie 0x8
ASMCONSTANTS_C_ASSERT(SIZEOF__GSCookie == sizeof(GSCookie));

Expand Down
16 changes: 0 additions & 16 deletions src/coreclr/vm/arm64/asmhelpers.S
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,6 @@ LEAF_ENTRY JIT_WriteBarrier_Callable, _TEXT
br x17
LEAF_END JIT_WriteBarrier_Callable, _TEXT

// void SinglecastDelegateInvokeStub(Delegate *pThis)
LEAF_ENTRY SinglecastDelegateInvokeStub, _TEXT
cmp x0, #0
beq LOCAL_LABEL(LNullThis)

ldr x16, [x0, #DelegateObject___methodPtr]
ldr x0, [x0, #DelegateObject___target]

br x16

LOCAL_LABEL(LNullThis):
mov x0, #CORINFO_NullReferenceException_ASM
b C_FUNC(JIT_InternalThrow)

LEAF_END SinglecastDelegateInvokeStub, _TEXT

//
// x12 = UMEntryThunk*
//
Expand Down
17 changes: 0 additions & 17 deletions src/coreclr/vm/arm64/asmhelpers.asm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
IMPORT PreStubWorker
IMPORT NDirectImportWorker
IMPORT VSD_ResolveWorker
IMPORT JIT_InternalThrow
IMPORT ComPreStubWorker
IMPORT COMToCLRWorker
IMPORT CallDescrWorkerUnwindFrameChainHandler
Expand Down Expand Up @@ -317,22 +316,6 @@ EphemeralCheckEnabled

LEAF_END JIT_UpdateWriteBarrierState

; void SinglecastDelegateInvokeStub(Delegate *pThis)
LEAF_ENTRY SinglecastDelegateInvokeStub
cmp x0, #0
beq LNullThis

ldr x16, [x0, #DelegateObject___methodPtr]
ldr x0, [x0, #DelegateObject___target]

br x16

LNullThis
mov x0, #CORINFO_NullReferenceException_ASM
b JIT_InternalThrow

LEAF_END

#ifdef FEATURE_COMINTEROP

; ------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/arm64/cgencpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,6 @@ class StubLinkerCPU : public StubLinker

};

extern "C" void SinglecastDelegateInvokeStub();


// preferred alignment for data
#define DATA_ALIGNMENT 8
Expand Down
Loading

0 comments on commit cc17a46

Please sign in to comment.