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

Add support for SwiftIndirectResult in Mono JIT and Interpreter #104111

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 25 additions & 2 deletions src/mono/mono/mini/mini-amd64.c
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
cinfo->nargs = n;
cinfo->gsharedvt = mini_is_gsharedvt_variable_signature (sig);
cinfo->swift_error_index = -1;
cinfo->swift_indirect_result_index = -1;

gr = 0;
fr = 0;
Expand Down Expand Up @@ -1014,17 +1015,23 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
if (mono_method_signature_has_ext_callconv (sig, MONO_EXT_CALLCONV_SWIFTCALL)) {
MonoClass *swift_self = mono_class_try_get_swift_self_class ();
MonoClass *swift_error = mono_class_try_get_swift_error_class ();
MonoClass *swift_indirect_result = mono_class_try_get_swift_indirect_result_class ();
MonoClass *swift_error_ptr = mono_class_create_ptr (m_class_get_this_arg (swift_error));
MonoClass *klass = mono_class_from_mono_type_internal (sig->params [i]);
if (klass == swift_self && sig->pinvoke) {
if (klass == swift_indirect_result)
cinfo->swift_indirect_result_index = i;

if ((klass == swift_self || klass == swift_indirect_result) && sig->pinvoke) {
guint32 size = mini_type_stack_size_full (m_class_get_byval_arg (klass), NULL, sig->pinvoke && !sig->marshalling_disabled);
g_assert (size == 8);

ainfo->storage = ArgValuetypeInReg;
ainfo->pair_storage [0] = ArgInIReg;
ainfo->pair_storage [1] = ArgNone;
ainfo->nregs = 1;
ainfo->pair_regs [0] = GINT32_TO_UINT8 (AMD64_R13);
// The indirect result is RAX on AMD64. However, since RAX is used as a scratch register,
matouskozak marked this conversation as resolved.
Show resolved Hide resolved
// the R10 register is used instead and before the native call, the value is moved from R10 to RAX.
matouskozak marked this conversation as resolved.
Show resolved Hide resolved
ainfo->pair_regs [0] = (klass == swift_self) ? GINT32_TO_UINT8 (AMD64_R13) : GINT32_TO_UINT8 (AMD64_R10);
ainfo->pair_size [0] = size;
continue;
} else if (klass == swift_error || klass == swift_error_ptr) {
Expand Down Expand Up @@ -5645,6 +5652,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
}

code = amd64_handle_varargs_call (cfg, code, call, TRUE);

#ifdef MONO_ARCH_HAVE_SWIFTCALL
// Ideally, this would be in mono_arch_emit_prolog, but amd64_handle_varargs_call is required before the move to free RAX.
if (mono_method_signature_has_ext_callconv (cfg->method->signature, MONO_EXT_CALLCONV_SWIFTCALL) &&
cfg->arch.cinfo->swift_indirect_result_index > -1) {
amd64_mov_reg_reg (code, AMD64_RAX, AMD64_R10, 8);
matouskozak marked this conversation as resolved.
Show resolved Hide resolved
}
#endif
amd64_call_reg (code, ins->sreg1);
ins->flags |= MONO_INST_GC_CALLSITE;
ins->backend.pc_offset = GPTRDIFF_TO_INT (code - cfg->native_code);
Expand Down Expand Up @@ -8187,6 +8202,14 @@ MONO_RESTORE_WARNING
amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
}

#ifdef MONO_ARCH_HAVE_SWIFTCALL
if (mono_method_signature_has_ext_callconv (sig, MONO_EXT_CALLCONV_SWIFTCALL) &&
cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED &&
cfg->arch.cinfo->swift_indirect_result_index > -1) {
amd64_mov_reg_reg (code, AMD64_R10, AMD64_RAX, 8);
}
#endif

/* Keep this in sync with emit_load_volatile_arguments */
for (guint i = 0; i < sig->param_count + sig->hasthis; ++i) {
ArgInfo *ainfo = cinfo->args + i;
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/mini/mini-amd64.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ struct CallInfo {
guint32 reg_usage;
guint32 freg_usage;
gint32 swift_error_index;
gint32 swift_indirect_result_index;
gboolean need_stack_align;
gboolean gsharedvt;
/* The index of the vret arg in the argument list */
Expand Down
5 changes: 5 additions & 0 deletions src/mono/mono/mini/tramp-amd64.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,8 @@ mono_arch_get_interp_to_native_trampoline (MonoTrampInfo **info)
amd64_mov_membase_reg (code, AMD64_RBP, off_ctxregs - i * sizeof (target_mgreg_t), i + CTX_REGS_OFFSET, sizeof (target_mgreg_t));
amd64_mov_reg_membase (code, i + CTX_REGS_OFFSET, AMD64_R11, MONO_STRUCT_OFFSET (CallContext, gregs) + (i + CTX_REGS_OFFSET) * sizeof (target_mgreg_t), sizeof (target_mgreg_t));
}
// Move R10 to RAX before the native call as R10 is used as a proxy for SwiftIndirectResult.
amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (CallContext, gregs) + 10 * sizeof (target_mgreg_t), sizeof (target_mgreg_t));
#endif

/* load target addr */
Expand Down Expand Up @@ -1267,6 +1269,9 @@ mono_arch_get_native_to_interp_trampoline (MonoTrampInfo **info)
/* set context registers to CallContext */
for (i = 0; i < CTX_REGS; i++)
amd64_mov_membase_reg (code, AMD64_RSP, ctx_offset + MONO_STRUCT_OFFSET (CallContext, gregs) + (i + CTX_REGS_OFFSET) * sizeof (target_mgreg_t), i + CTX_REGS_OFFSET, sizeof (target_mgreg_t));

// Move RAX to R10 before the managed call as R10 is used as a proxy for SwiftIndirectResult.
amd64_mov_membase_reg (code, AMD64_RSP, ctx_offset + MONO_STRUCT_OFFSET (CallContext, gregs) + 10 * sizeof (target_mgreg_t), AMD64_RAX, sizeof (target_mgreg_t));
matouskozak marked this conversation as resolved.
Show resolved Hide resolved
#endif

/* set the stack pointer to the value at call site */
Expand Down