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

[mono][aot] Prefer specific instances instead of gshared for methods … #76033

Merged
merged 1 commit into from
Sep 27, 2022
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
62 changes: 59 additions & 3 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ typedef struct MonoAotCompile {
GList *profile_data;
GHashTable *profile_methods;
GHashTable *blob_hash;
/* Maps MonoMethod*->GPtrArray* */
GHashTable *gshared_instances;
/* Hash of gshared methods where specific instances are preferred */
GHashTable *prefer_instances;
#ifdef EMIT_WIN32_UNWIND_INFO
GList *unwind_info_section_cache;
#endif
Expand Down Expand Up @@ -4302,8 +4306,23 @@ mono_dedup_cache_method (MonoAotCompile *acfg, MonoMethod *method)
g_hash_table_insert (acfg->dedup_stats, stats_name, GUINT_TO_POINTER (count));
}

static gboolean
is_open_method (MonoMethod *method)
{
MonoGenericContext *context;

if (!method->is_inflated)
return FALSE;
context = mono_method_get_context (method);
if (context->class_inst && context->class_inst->is_open)
return TRUE;
if (context->method_inst && context->method_inst->is_open)
return TRUE;
return FALSE;
}

static void
add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth)
add_extra_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean prefer_gshared, int depth)
{
ERROR_DECL (error);

Expand All @@ -4315,7 +4334,7 @@ add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth
return;
}

if (mono_method_is_generic_sharable_full (method, TRUE, TRUE, FALSE)) {
if (prefer_gshared && mono_method_is_generic_sharable_full (method, TRUE, TRUE, FALSE)) {
MonoMethod *orig = method;

method = mini_get_shared_method_full (method, SHARE_MODE_NONE, error);
Expand All @@ -4325,10 +4344,23 @@ add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth
return;
}

if (g_hash_table_lookup (acfg->prefer_instances, method))
/* Compile an instance as well */
add_extra_method_full (acfg, orig, FALSE, depth);

/* Add it to profile_methods so its not skipped later */
if (acfg->aot_opts.profile_only && g_hash_table_lookup (acfg->profile_methods, orig))
g_hash_table_insert (acfg->profile_methods, method, method);
} else if ((acfg->jit_opts & MONO_OPT_GSHAREDVT) && prefer_gsharedvt_method (acfg, method) && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {

if (!is_open_method (orig) && !mono_method_is_generic_sharable_full (orig, TRUE, FALSE, FALSE)) {
GPtrArray *instances = g_hash_table_lookup (acfg->gshared_instances, method);
if (!instances) {
instances = g_ptr_array_new ();
g_hash_table_insert (acfg->gshared_instances, method, instances);
}
g_ptr_array_add (instances, orig);
}
} else if ((acfg->jit_opts & MONO_OPT_GSHAREDVT) && prefer_gshared && prefer_gsharedvt_method (acfg, method) && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
/* Use the gsharedvt version */
method = mini_get_shared_method_full (method, SHARE_MODE_GSHAREDVT, error);
mono_error_assert_ok (error);
Expand All @@ -4347,6 +4379,12 @@ add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth
add_method_full (acfg, method, TRUE, depth);
}

static void
add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth)
{
add_extra_method_full (acfg, method, TRUE, depth);
}

static void
add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
{
Expand Down Expand Up @@ -9209,6 +9247,22 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
printf ("%s ### %d\n", mono_method_get_full_name (method), cfg->code_size);
}

if (cfg->prefer_instances) {
/*
* Compile the original specific instances in addition to the gshared method
* for performance reasons, since gshared methods cannot implement some
* features like static virtual methods efficiently.
*/
g_hash_table_insert (acfg->prefer_instances, method, method);
GPtrArray *instances = g_hash_table_lookup (acfg->gshared_instances, method);
if (instances) {
for (guint i = 0; i < instances->len; ++i) {
MonoMethod *instance = (MonoMethod*)g_ptr_array_index (instances, i);
add_extra_method_full (acfg, instance, FALSE, 0);
}
}
}

/* Adds generic instances referenced by this method */
/*
* The depth is used to avoid infinite loops when generic virtual recursion is
Expand Down Expand Up @@ -13631,6 +13685,8 @@ acfg_create (MonoAssembly *ass, guint32 jit_opts)
acfg->gsharedvt_in_signatures = g_hash_table_new ((GHashFunc)mono_signature_hash, (GEqualFunc)mono_metadata_signature_equal);
acfg->gsharedvt_out_signatures = g_hash_table_new ((GHashFunc)mono_signature_hash, (GEqualFunc)mono_metadata_signature_equal);
acfg->profile_methods = g_hash_table_new (NULL, NULL);
acfg->gshared_instances = g_hash_table_new (NULL, NULL);
acfg->prefer_instances = g_hash_table_new (NULL, NULL);
mono_os_mutex_init_recursive (&acfg->mutex);

init_got_info (&acfg->got_info);
Expand Down
10 changes: 7 additions & 3 deletions src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -7493,10 +7493,14 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
if (constrained_class) {
if (m_method_is_static (cil_method) && mini_class_check_context_used (cfg, constrained_class)) {
/* get_constrained_method () doesn't work on the gparams used by generic sharing */
// FIXME: Other configurations
//if (!cfg->gsharedvt)
// GENERIC_SHARING_FAILURE (CEE_CALL);
gshared_static_virtual = TRUE;
if (!cfg->gsharedvt)
/*
* We can't resolve these calls at compile time, and they are used in
* perf-sensitive code in the BCL, so ask the AOT compiler to try to use specific instances
* instead of this gshared method.
*/
cfg->prefer_instances = TRUE;
} else {
cmethod = get_constrained_method (cfg, image, token, cil_method, constrained_class, generic_context);
CHECK_CFG_ERROR;
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/mini/mini.h
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@ typedef struct {
guint after_method_to_ir : 1;
guint disable_inline_rgctx_fetch : 1;
guint deopt : 1;
guint prefer_instances : 1;
guint8 uses_simd_intrinsics;
int r4_stack_type;
gpointer debug_info;
Expand Down