From 3769b5e1529f76373c06c29f315e40982cc85250 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Mon, 31 Jul 2023 10:48:51 -0400 Subject: [PATCH] [mono][aot] Prefer instances instead of using gsharedvt for types inflated with empty structs. (#89688) These structs are used in corlib for compile time templating. Fixes https://github.com/dotnet/runtime/issues/89624. --- src/mono/mono/mini/aot-compiler.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/mono/mono/mini/aot-compiler.c b/src/mono/mono/mini/aot-compiler.c index c2b816c60b383..c3568f34a7fd8 100644 --- a/src/mono/mono/mini/aot-compiler.c +++ b/src/mono/mono/mini/aot-compiler.c @@ -5541,12 +5541,18 @@ is_vt_inst (MonoGenericInst *inst) } static gboolean -is_vt_inst_no_enum (MonoGenericInst *inst) +is_vt_inst_no_enum_not_empty (MonoGenericInst *inst) { for (guint i = 0; i < inst->type_argc; ++i) { MonoType *t = inst->type_argv [i]; - if (MONO_TYPE_ISSTRUCT (t)) - return TRUE; + if (MONO_TYPE_ISSTRUCT (t)) { + MonoClass *k = mono_class_from_mono_type_internal (t); + /* + * Empty vtypes with static virtual methods are used for templating in corlib. + */ + if (mono_class_get_field_count (k) > 0) + return TRUE; + } } return FALSE; } @@ -5682,7 +5688,7 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth, * WASM only since other platforms depend on the * previous behavior. */ - if ((acfg->jit_opts & MONO_OPT_GSHAREDVT) && mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst && is_vt_inst_no_enum (mono_class_get_generic_class (klass)->context.class_inst)) { + if ((acfg->jit_opts & MONO_OPT_GSHAREDVT) && mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst && is_vt_inst_no_enum_not_empty (mono_class_get_generic_class (klass)->context.class_inst)) { use_gsharedvt = TRUE; use_gsharedvt_for_array = TRUE; }