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][jit] Emit instances when using unmanaged generic calli in gsha… #99381

Merged
merged 2 commits into from
Mar 8, 2024
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
4 changes: 2 additions & 2 deletions src/mono/mono/metadata/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3922,14 +3922,14 @@ mono_marshal_get_native_func_wrapper_indirect (MonoClass *caller_class, MonoMeth
caller_class = mono_class_get_generic_type_definition (caller_class);
MonoImage *image = m_class_get_image (caller_class);
g_assert (sig->pinvoke);
g_assert (!sig->hasthis && ! sig->explicit_this);
g_assert (!sig->hasthis && !sig->explicit_this);
g_assert (!sig->has_type_parameters);

#if 0
/*
* Since calli sigs are already part of ECMA-335, they were already used by C++/CLI, which
* allowed non-blittable types. So the C# function pointers spec doesn't restrict this to
* blittable tyhpes only.
* blittable types only.
*/
g_assertf (type_is_blittable (sig->ret), "sig return type %s is not blittable\n", mono_type_full_name (sig->ret));

Expand Down
34 changes: 17 additions & 17 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -9541,6 +9541,23 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
cfg = mini_method_compile (method, acfg->jit_opts, flags, 0, index);
mono_time_track_end (&mono_jit_stats.jit_time, jit_time_start);

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.
*/
/* Instances encountered later will be handled in add_extra_method_full () */
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);
}
}
}

if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
if (acfg->aot_opts.print_skipped_methods)
printf ("Skip (gshared failure): %s (%s)\n", mono_method_get_full_name (method), cfg->exception_message);
Expand Down Expand Up @@ -9626,23 +9643,6 @@ 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.
*/
/* Instances encountered later will be handled in add_extra_method_full () */
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
4 changes: 3 additions & 1 deletion src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -7548,8 +7548,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
if (cfg->compile_aot)
cfg->pinvoke_calli_signatures = g_slist_prepend_mempool (cfg->mempool, cfg->pinvoke_calli_signatures, fsig);

if (fsig->has_type_parameters)
if (fsig->has_type_parameters) {
cfg->prefer_instances = TRUE;
GENERIC_SHARING_FAILURE (CEE_CALLI);
}

/* Call the wrapper that will do the GC transition instead */
MonoMethod *wrapper = mono_marshal_get_native_func_wrapper_indirect (method->klass, fsig, cfg->compile_aot);
Expand Down
2 changes: 0 additions & 2 deletions src/tests/Interop/Interop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
<Compile Include="LayoutClass/*.cs" />
<Compile Include="ArrayMarshalling/**/*.cs" />
<Compile Include="MarshalAPI/**/*.cs" />
<!-- Mono support: https://github.com/dotnet/runtime/issues/98819 -->
<Compile Condition="'$(RuntimeFlavor)' == 'mono'" Remove="MarshalAPI/FunctionPointer/GenericFunctionPointer.cs" />
<Compile Include="PInvoke/Array/**/*.cs" />
<Compile Include="PInvoke/ArrayWithOffset/**/*.cs" />
<Compile Include="PInvoke/AsAny/**/*.cs" />
Expand Down
Loading