Skip to content

JIT/AOT to interpreter calls support #116353

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

Merged
merged 9 commits into from
Jun 9, 2025
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
5 changes: 5 additions & 0 deletions src/coreclr/interpreter/interpretershared.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#define INTERP_INDIRECT_HELPER_TAG 1 // When a helper ftn's address is indirect we tag it with this tag bit

struct CallStubHeader;

struct InterpMethod
{
#if DEBUG
Expand All @@ -27,6 +29,8 @@ struct InterpMethod
CORINFO_METHOD_HANDLE methodHnd;
int32_t allocaSize;
void** pDataItems;
// This stub is used for calling the interpreted method from JITted/AOTed code
CallStubHeader *pCallStub;
bool initLocals;

InterpMethod(CORINFO_METHOD_HANDLE methodHnd, int32_t allocaSize, void** pDataItems, bool initLocals)
Expand All @@ -38,6 +42,7 @@ struct InterpMethod
this->allocaSize = allocaSize;
this->pDataItems = pDataItems;
this->initLocals = initLocals;
pCallStub = NULL;
}

bool CheckIntegrity()
Expand Down
38 changes: 30 additions & 8 deletions src/coreclr/pal/inc/unixasmmacrosamd64.inc
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ C_FUNC(\Name\()_End):

.endm

.macro SKIP_ARGUMENT_REGISTERS

add rsp, 6 * 8

.endm

.macro SAVE_FLOAT_ARGUMENT_REGISTERS ofs

save_xmm128_postrsp xmm0, \ofs
Expand Down Expand Up @@ -344,7 +350,7 @@ C_FUNC(\Name\()_End):
.macro EPILOG_WITH_TRANSITION_BLOCK_RETURN

free_stack __PWTB_StackAlloc
POP_ARGUMENT_REGISTERS
SKIP_ARGUMENT_REGISTERS
POP_CALLEE_SAVED_REGISTERS
ret

Expand All @@ -368,19 +374,29 @@ C_FUNC(\Name\()_End):

.endm

// Inlined version of GetThreadEEAllocContext. Trashes volatile registers.
.macro INLINE_GET_ALLOC_CONTEXT_BASE
#if defined(FEATURE_EMULATED_TLS) || defined(__APPLE__)
call C_FUNC(GetThreadEEAllocContext)
.macro INLINE_GET_TLS_VAR Var
.att_syntax
#if defined(__APPLE__)
movq _\Var@TLVP(%rip), %rdi
callq *(%rdi)
#else
.att_syntax
.byte 0x66 // data16 prefix - padding to have space for linker relaxations
leaq t_runtime_thread_locals@TLSGD(%rip), %rdi
leaq \Var@TLSGD(%rip), %rdi
.byte 0x66 //
.byte 0x66 //
.byte 0x48 // rex.W prefix, also for padding
callq __tls_get_addr@PLT
.intel_syntax noprefix
#endif
.intel_syntax noprefix
.endm


// Inlined version of GetThreadEEAllocContext. Trashes volatile registers.
.macro INLINE_GET_ALLOC_CONTEXT_BASE
#ifdef FEATURE_EMULATED_TLS
call C_FUNC(GetThreadEEAllocContext)
#else
INLINE_GET_TLS_VAR t_runtime_thread_locals

.ifnc OFFSETOF__RuntimeThreadLocals__ee_alloc_context, 0
lea rax, [rax + OFFSETOF__RuntimeThreadLocals__ee_alloc_context]
Expand All @@ -405,3 +421,9 @@ C_FUNC(\Name\()_End):
free_stack 56
POP_CALLEE_SAVED_REGISTERS
.endm

.macro INLINE_GETTHREAD
// Inlined version of call C_FUNC(RhpGetThread)
INLINE_GET_TLS_VAR t_CurrentThreadInfo
mov rax, [rax + OFFSETOF__ThreadLocalInfo__m_pThread]
.endm
67 changes: 52 additions & 15 deletions src/coreclr/pal/inc/unixasmmacrosarm64.inc
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ C_FUNC(\Name\()_End):
// ArgumentRegisters::x2
// ArgumentRegisters::x1
// ArgumentRegisters::x0
// ArgumentRegisters::x8
// FloatRegisters::q7
// FloatRegisters::q6
// FloatRegisters::q5
Expand All @@ -192,7 +193,7 @@ C_FUNC(\Name\()_End):
// FloatRegisters::q2
// FloatRegisters::q1
// FloatRegisters::q0
.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, SaveFPArgs = 1
.macro PROLOG_WITH_TRANSITION_BLOCK extraLocals = 0, SaveFPArgs = 1, SaveGPArgs = 1

__PWTB_FloatArgumentRegisters = \extraLocals
__PWTB_SaveFPArgs = \SaveFPArgs
Expand Down Expand Up @@ -222,8 +223,10 @@ C_FUNC(\Name\()_End):
// Allocate space for the rest of the frame
PROLOG_STACK_ALLOC __PWTB_StackAlloc

// Spill argument registers.
SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters
.if (\SaveGPArgs == 1)
// Spill argument registers.
SAVE_ARGUMENT_REGISTERS sp, __PWTB_ArgumentRegisters
.endif

.if (__PWTB_SaveFPArgs == 1)
SAVE_FLOAT_ARGUMENT_REGISTERS sp, \extraLocals
Expand Down Expand Up @@ -301,7 +304,6 @@ C_FUNC(\Name\()_End):

.endm


//-----------------------------------------------------------------------------
// Provides a matching epilog to PROLOG_WITH_TRANSITION_BLOCK and ends by preparing for tail-calling.
// Since this is a tail call argument registers are restored.
Expand All @@ -325,6 +327,41 @@ C_FUNC(\Name\()_End):

.endm

// Loads the address of a thread-local variable into the target register,
// which cannot be x0.
// Preserves registers except for xip0 and xip1 on Apple
.macro INLINE_GET_TLS_VAR target, var
.ifc \target, x0
.error "target cannot be x0"
.endif

// This sequence of instructions is recognized and potentially patched
// by the linker (GD->IE/LE relaxation).
#if defined(__APPLE__)

adrp x0, \var@TLVPPAGE
ldr x0, [x0, \var@TLVPPAGEOFF]
ldr \target, [x0]

blr \target
// End of the sequence

mov \target, x0
#else
adrp x0, :tlsdesc:\var
ldr \target, [x0, #:tlsdesc_lo12:\var]
add x0, x0, :tlsdesc_lo12:\var
.tlsdesccall \var
blr \target
// End of the sequence

mrs \target, tpidr_el0
add \target, \target, x0
#endif

.endm


// Inlined version of GetThreadEEAllocContext. Target cannot be x0 or x1.
.macro INLINE_GET_ALLOC_CONTEXT_BASE target
.ifc \target, x0
Expand All @@ -345,17 +382,7 @@ C_FUNC(\Name\()_End):
EPILOG_RESTORE_REG_PAIR_INDEXED fp, lr, 0x20
#else
PROLOG_SAVE_REG_PAIR_INDEXED x0, lr, -0x10

// This sequence of instructions is recognized and potentially patched
// by the linker (GD->IE/LE relaxation).
adrp x0, :tlsdesc:t_runtime_thread_locals
ldr \target, [x0, :tlsdesc_lo12:t_runtime_thread_locals]
add x0, x0, :tlsdesc_lo12:t_runtime_thread_locals
blr \target
// End of the sequence

mrs \target, TPIDR_EL0
add \target, \target, x0
INLINE_GET_TLS_VAR \target, t_runtime_thread_locals

.ifnc OFFSETOF__RuntimeThreadLocals__ee_alloc_context, 0
add \target, x0, OFFSETOF__RuntimeThreadLocals__ee_alloc_context
Expand Down Expand Up @@ -470,3 +497,13 @@ $__RedirectionStubEndFuncName
0:
#endif
.endm

#define xip0 x16
#define xip1 x17
#define xpr x18

// Inlined version of RhpGetThread. Target cannot be x0.
.macro INLINE_GETTHREAD target
INLINE_GET_TLS_VAR \target, C_FUNC(t_CurrentThreadInfo)
ldr \target, [\target, #OFFSETOF__ThreadLocalInfo__m_pThread]
.endm
Loading