Skip to content

[mono] Don't use direct wrapper when invoking cctors, so all cctors can share one wrapper #102375

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ export function generateWasmBody (
// TODO: Verify that this isn't worse. I think these may only show up in wrappers?
// case MintOpcode.MINT_JIT_CALL:
case MintOpcode.MINT_CALLI:
case MintOpcode.MINT_CALLI_MONOMETHOD:
case MintOpcode.MINT_CALLI_NAT:
case MintOpcode.MINT_CALLI_NAT_DYNAMIC:
case MintOpcode.MINT_CALLI_NAT_FAST:
Expand Down
40 changes: 23 additions & 17 deletions src/mono/mono/metadata/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3523,26 +3523,31 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
ERROR_DECL (emitted_error);

g_assert (method != NULL);
g_assertf (mono_method_signature_internal (method)->pinvoke, "%s flags:%X iflags:%X param_count:%X",
method->name, method->flags, method->iflags, mono_method_signature_internal (method)->param_count);
sig = mono_method_signature_internal (method);
g_assert (sig != NULL);
g_assertf (sig->pinvoke, "%s flags:%X iflags:%X param_count:%X",
method->name, method->flags, method->iflags, sig->param_count);

if (MONO_CLASS_IS_IMPORT (method->klass)) {
mono_error_set_generic_error (emitted_error, "System", "PlatformNotSupportedException", "Built-in COM interop is not supported on Mono");
}

MonoWrapperCaches *wrapper_cache = mono_method_get_wrapper_cache (method);
g_assert (wrapper_cache);
if (aot) {
if (check_exceptions)
cache_ptr = &mono_method_get_wrapper_cache (method)->native_wrapper_aot_check_cache;
cache_ptr = &wrapper_cache->native_wrapper_aot_check_cache;
else
cache_ptr = &mono_method_get_wrapper_cache (method)->native_wrapper_aot_cache;
cache_ptr = &wrapper_cache->native_wrapper_aot_cache;
} else {
if (check_exceptions)
cache_ptr = &mono_method_get_wrapper_cache (method)->native_wrapper_check_cache;
cache_ptr = &wrapper_cache->native_wrapper_check_cache;
else
cache_ptr = &mono_method_get_wrapper_cache (method)->native_wrapper_cache;
cache_ptr = &wrapper_cache->native_wrapper_cache;
}

cache = get_cache (cache_ptr, mono_aligned_addr_hash, NULL);
g_assert (cache);

if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
Expand All @@ -3555,12 +3560,12 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
}
}

sig = mono_method_signature_internal (method);

if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
pinvoke = TRUE;

g_assert (piinfo);

if (!piinfo->addr) {
if (pinvoke) {
if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
Expand All @@ -3573,6 +3578,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
}

mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
g_assert (mb);
mb->method->save_lmf = 1;

if (G_UNLIKELY (pinvoke && mono_method_has_unmanaged_callers_only_attribute (method))) {
Expand Down Expand Up @@ -5306,7 +5312,7 @@ mono_marshal_get_unsafe_accessor_wrapper (MonoMethod *accessor_method, MonoUnsaf
return res;
}
// printf ("Cache miss\n");

mb = mono_mb_new (accessor_method->klass, accessor_method->name, MONO_WRAPPER_OTHER);
if (generic_wrapper) {
// If the accessor method was generic, make the wrapper generic, too.
Expand Down Expand Up @@ -6707,16 +6713,16 @@ static int get_swift_lowering_alignment (SwiftPhysicalLoweringKind kind) {

static void set_lowering_range(guint8* lowered_bytes, guint32 offset, guint32 size, SwiftPhysicalLoweringKind kind) {
bool force_opaque = false;

if (offset != ALIGN_TO(offset, get_swift_lowering_alignment(kind))) {
// If the start of the range is not aligned, we need to force the entire range to be opaque.
force_opaque = true;
}

// Check if any of the range is non-empty.
// If so, we need to force this range to be opaque
// and extend the range to the existing tag's range and mark as opaque in addition to the requested range.

for (guint32 i = 0; i < size; ++i) {
SwiftPhysicalLoweringKind current = (SwiftPhysicalLoweringKind)lowered_bytes[offset + i];
if (current != SWIFT_EMPTY && current != kind) {
Expand Down Expand Up @@ -6785,7 +6791,7 @@ static void record_struct_field_physical_lowering (guint8* lowered_bytes, MonoTy
else if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR
|| type->type == MONO_TYPE_I || type->type == MONO_TYPE_U) {
kind = SWIFT_INT64;
}
}
#endif
else if (type->type == MONO_TYPE_R4) {
kind = SWIFT_FLOAT;
Expand Down Expand Up @@ -6832,7 +6838,7 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
}

guint8 lowered_bytes[TARGET_SIZEOF_VOID_P * 4] = { 0 };

// Loop through all fields and get the physical lowering for each field
record_struct_physical_lowering(lowered_bytes, klass, 0);

Expand Down Expand Up @@ -6863,7 +6869,7 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
|| (i == ALIGN_TO(i, 8) && (current == SWIFT_DOUBLE || current == SWIFT_INT64))
// We've changed interval types
|| current != lowered_bytes[i - 1];

if (start_new_interval) {
struct _SwiftInterval interval = { i, 1, current };
g_array_append_val(intervals, interval);
Expand All @@ -6889,7 +6895,7 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
MonoTypeEnum lowered_types[4];
guint32 offsets[4];
guint32 num_lowered_types = 0;

for (int i = 0; i < intervals->len; ++i) {
if (num_lowered_types == 4) {
// We can't handle more than 4 fields
Expand All @@ -6899,7 +6905,7 @@ mono_marshal_get_swift_physical_lowering (MonoType *type, gboolean native_layout
}

struct _SwiftInterval interval = g_array_index(intervals, struct _SwiftInterval, i);

offsets[num_lowered_types] = interval.start;

switch (interval.kind) {
Expand Down
19 changes: 18 additions & 1 deletion src/mono/mono/mini/interp/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2177,14 +2177,18 @@ interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject
MonoMethodSignature *sig = mono_method_signature_internal (method);
stackval *sp = (stackval*)context->stack_pointer;
MonoMethod *target_method = method;
gboolean need_direct_wrapper = TRUE;

error_init (error);
if (exc)
*exc = NULL;

if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
MonoMethod *invoke_wrapper = mono_marshal_get_runtime_invoke_full (target_method, FALSE, TRUE);
if ((sig->param_count == 0) && !sig->hasthis && !strcmp(method->name, ".cctor"))
need_direct_wrapper = FALSE;

MonoMethod *invoke_wrapper = mono_marshal_get_runtime_invoke_full (target_method, FALSE, need_direct_wrapper);

//* <code>MonoObject *runtime_invoke (MonoObject *this_obj, void **params, MonoObject **exc, void* method)</code>

Expand Down Expand Up @@ -4181,6 +4185,19 @@ mono_interp_exec_method (InterpFrame *frame, ThreadContext *context, FrameClause

goto jit_call;
}
MINT_IN_CASE(MINT_CALLI_MONOMETHOD) {
MonoMethod *mm = (MonoMethod *)LOCAL_VAR (ip [2], gpointer);
g_assert (mm);
cmethod = mono_interp_get_imethod (mm);
g_assert (cmethod);
g_assert (!(cmethod->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));

return_offset = ip [1];
call_args_offset = ip [3];

ip += 4;
goto jit_call;
}
MINT_IN_CASE(MINT_CALLI_NAT_FAST) {
MintICallSig icall_sig = (MintICallSig)ip [4];
MonoMethodSignature *csignature = (MonoMethodSignature*)frame->imethod->data_items [ip [5]];
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/mini/interp/mintops.def
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ OPDEF(MINT_CALL, "call", 4, 1, 1, MintOpMethodToken)
OPDEF(MINT_CALLVIRT_FAST, "callvirt.fast", 5, 1, 1, MintOpMethodToken)
OPDEF(MINT_CALL_DELEGATE, "call.delegate", 6, 1, 1, MintOpTwoShorts)
OPDEF(MINT_CALLI, "calli", 4, 1, 2, MintOpNoArgs)
OPDEF(MINT_CALLI_MONOMETHOD, "calli.monomethod", 4, 1, 2, MintOpNoArgs)
OPDEF(MINT_CALLI_NAT, "calli.nat", 8, 1, 2, MintOpTwoShorts)
OPDEF(MINT_CALLI_NAT_DYNAMIC, "calli.nat.dynamic", 5, 1, 2, MintOpShortInt)
OPDEF(MINT_CALLI_NAT_FAST, "calli.nat.fast", 7, 1, 2, MintOpTwoShorts)
Expand Down
33 changes: 31 additions & 2 deletions src/mono/mono/mini/interp/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -3356,7 +3356,24 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
* The compiled interp entry wrapper is passed to runtime_invoke instead of
* the InterpMethod pointer. FIXME
*/
native = csignature->pinvoke || method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE;
native = csignature->pinvoke;
if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
if (target_method && mono_interp_jit_call_supported (target_method, csignature)) {
if (td->verbose_level > 2) {
g_printf (
"Enabling native invoke for runtime invoke wrapper because jit_call is supported for target %s\n",
target_method->name
);
native = 1;
}
} else if (td->verbose_level > 1) {
g_printf (
"Disabling native invoke for runtime invoke wrapper because jit_call is not supported for target %s\n",
target_method ? target_method->name : "(No target method)"
);
}
}

if (!method->dynamic && !method->wrapper_type && csignature->pinvoke && !mono_method_signature_has_ext_callconv (csignature, MONO_EXT_CALLCONV_SUPPRESS_GC_TRANSITION)) {
// native calli needs a wrapper
target_method = mono_marshal_get_native_func_wrapper_indirect (method->klass, csignature, FALSE);
Expand Down Expand Up @@ -3844,7 +3861,13 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
/* Cache slot */
td->last_ins->data [3] = get_data_item_index_nonshared (td, NULL);
} else {
interp_add_ins (td, MINT_CALLI);
if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
if (td->verbose_level > 1)
g_print ("Using MINT_CALLI_MONOMETHOD for runtime invoke wrapper\n");
interp_add_ins (td, MINT_CALLI_MONOMETHOD);
} else {
interp_add_ins (td, MINT_CALLI);
}
interp_ins_set_dreg (td->last_ins, dreg);
interp_ins_set_sregs2 (td->last_ins, fp_sreg, MINT_CALL_ARGS_SREG);
}
Expand Down Expand Up @@ -9162,6 +9185,12 @@ generate (MonoMethod *method, MonoMethodHeader *header, InterpMethod *rtm, MonoG
}
}

if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
// HACK: Enable verbose for runtime invoke wrappers since I am making changes there, and if we
// get CI failures I want diagnostic output in the log
// td->verbose_level = 2;
}

interp_method_compute_offsets (td, rtm, mono_method_signature_internal (method), header, error);
goto_if_nok (error, exit);

Expand Down
Loading