-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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] generic wrapper methods for unsafe accessors #101732
Conversation
a47da65
to
ca36798
Compare
0149584
to
817356b
Compare
if someone needs the unsafe accessor, lookup the wrapper instead. Needed when there's a call for extra instances
4a9bcb4
to
db8cbad
Compare
As long as the target is just some type that mentions a generic field, we're ok - the regular gsharing ldflda works. It just can't be a type variable.
When we create generic wrappers (or wrappers in a generic class), if the wrapper data needs to refer to a field, method, or parameter type of the definition, that data might need to be inflated if the containing class is inflated (or the generic wrapper method is inflated). Add a new function to opt into inflation: ```c get_marshal_cb ()->mb_inflate_wrapper_data (mb); ``` Add a new function to be called after mono_mb_emit_op (or any other call that calls mono_mb_add_data): ```c mono_mb_emit_op (mb, CEE_LDFLDA, field); mono_mb_set_wrapper_data_kind (mb, MONO_MB_ILGEN_WRAPPER_DATA_FIELD); ``` Note: mono_mb_set_wrapper_data_kind asserts if you haven't called mb_inflate_wrapper_data. TODO: add more wrapper data kinds for MonoMethod* and MonoClass* etc
Try to separate the ideas of "the call to the UnsafeAccessor method was inflated, so we need to inflate the wrapper" and "the UnsafeAccessor method is a generic method definition, so the wrapper should be a generic method definition, too"
/azp run runtime-llvm |
Azure Pipelines successfully started running 1 pipeline(s). |
6276119
to
c3dccc3
Compare
c3dccc3
to
25f9e99
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reviewed the unsafe accessor related change. Will review the method builder related change then AOT related change.
mb = mono_mb_new (accessor_method->klass, accessor_method->name, MONO_WRAPPER_OTHER); | ||
MonoGenericContext inst_ctx = {0,}; | ||
// FIXME: if is_inflated, do we need to mess with ctx? | ||
inst_ctx.method_inst = container->context.method_inst; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why only the method context is set here? What about the class context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So conceptually if both the class is generic and the static extern
accessor method is generic, when we make a wrapper we want its signature to use the same exact class gparams as the accessor method (conceptually both the accessor method and the wrapper are members of the class). But we want it to use its own brand new method gparams. So we take the original signature of hte access method and replace the method gparams:
class G<T> {
public static extern Tuple<T,U> Accessor<U>(T t, U u);
// wrapper:
// public static extern Tuple<T, U2> wrapper_Accessor<U2>(T t, U2 u);
}
so it's as if the wrapper instead of having a gparam named U
has a gparam named U2
. And so if we just take signature of the accessor, we have to replace all U
s by U2
. But the T
is the same
// we can't set inflate_wrapper_data to 0 on the result, it's possible it | ||
// will need to be inflated again (for example in the method_inst == | ||
// generic_container->context.method_inst case, below) | ||
resw->inflate_wrapper_data = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Consistency between 1
and TRUE
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MonoMethodWrapper:inflate_wrapper_data
is a bitfield, so I use 1
MonoMethodBuilder:inflate_wrapper_data
is a gboolean, so I use TRUE
generic_wrapper = TRUE; | ||
} | ||
|
||
if (is_inflated) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_inflated
appears redundant since accessor_method
is already passed to the emit_unsafe_accessor_[field/ctor/method]_wrapper
function.
Can accessor_method->is_inflated
be used directly instead of the is_inflated
variable? The idea is to make the wrapper inflated if the accessor method is inflated and remove gboolean to_be_inflated
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not redundant. Note that below on line 5272 we reassign accessor_method
to be the declaring method. So we would have to switch from accessor_method->is_inflated
to orig_method->is_inflated
in this function. And down in emit_unsafe_accessor_wrapper
we don't have orig_method
available.
Honestly what I would prefer is to make emit_unsafe_accessor_wrapper
not to take an accessor_method
argument at all. We can figure out the target class from the signature, and if we need to inflate the target method we can figure that out from the generic context. But there's other (mostly error checking) stuff that we can only get from accessor_method
: like whether it's static.
But there's a good point here: I always just need to know gboolean inflate_generic_data = accessor_method->is_generic || is_inflated
in emit_unsafe_accessor_{ctor/method/field}_wrapper
. So I will update marshal.c to just pass that down, instead of computing it in three different places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are my feedback for the method builder part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are my feedbacks for the aot part of change.
char *member_name = NULL; | ||
int accessor_kind = -1; | ||
if (mono_method_get_unsafe_accessor_attr_data (method, &accessor_kind, &member_name, error)) { | ||
MonoMethod *wrapper = mono_marshal_get_unsafe_accessor_wrapper (method, (MonoUnsafeAccessorKind)accessor_kind, member_name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why inflation is not needed here? i.e. calling replace_generated_method
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is iterating over the method definitions in the MONO_TABLE_METHOD
table. None of them are inflated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making this infrastructure change!
@@ -5049,24 +5068,63 @@ mono_aot_get_method (MonoMethod *method, MonoError *error) | |||
method_index = find_aot_method (shared, &amodule); | |||
if (method_index != 0xffffff) | |||
method = shared; | |||
if (method_index == 0xffffff && !mono_method_metadata_has_header (method)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it true that method without header will always be extern method with unsafe accessor attribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The overall structure of mono_aot_get_method
is that it keeps trying different ways to find the method that it needs while method_index == 0xffffff
(which means it hasn't found one yet).
So when mini_replace_generated_method
succeeds, it will return some method and we will try to find its method_index.
If mini_replace_generated_method
doesn't find something that it can work on, it will return NULL
and we will keep method_index
still as 0xffffff
and mono_aot_get_method
will keep trying other ways.
mini_replace_generated_method
is a little bit expensive - looking up a custom attribute is expensive. So we don't do it for every method
. We only do it for the ones where there's no method header. Checking if there's a header is much cheaper than trying to find the UnsafeAccessorAttribute on every single method.
There might be other kinds of methods that have no method header. but we don't care about that - in those cases mini_replace_generated_method
will return NULL and we'll keep going.
1. Adds support for compiling generic wrapper methods to Mono. In some situations we need to emit a wrapper method that is generic. This makes the MethodBuilder infrastructure support that. 2. Adds support for inflating generic wrapper data. Wrappers have pointer data associated with them that is used by the code generator (For example instead of emitting field tokens, we record the `MonoClassField*` directly and then emit a fake token that is just the index of the `MonoClassField*` in the `MonoMethodWrapper:method_data` array). The pointer data associated with a wrapper is normally just consumed verbatim. But if the wrapper is part of a generic class, or if the wrapper is a generic method, the wrapper data might have generic parameters (for example it might be a MonoClassField for `MyList<T>` instead of `MyList<string>`). Add support for tagging the data with its kind and inflating it if the wrapper method is inflated. 3. Applies (1) and (2) to unsafe accessor methods - the unsafe accesor wrapper generation now always tries to get the generic definition and to generate a wrapper for that generic definition and then inflate it. 4. Some AOT changes so that FullAOT substitutes lookups for an unsafe accessor by lookups for the wrapper. Including if the unsafe accessor or wrapper is generic. This also enabled gshared and gsharedvt for unsafe accessor wrappers. This also fixes dotnet#92883 Contributes to dotnet#99830, dotnet#89439 **NOT DONE** - We don't check constraints on the generic target types yet --- * always AOT wrappers, even for generics, not the actual accessor * add generic wrapper methods * use generic method owner caches * lookup unsafe accessor wrapper instances in aot-runtime if someone needs the unsafe accessor, lookup the wrapper instead. Needed when there's a call for extra instances * cleanup marshaling - dont' use ctx as a flag * handle some generic field accessors As long as the target is just some type that mentions a generic field, we're ok - the regular gsharing ldflda works. It just can't be a type variable. * issues.targets: enable some unsafe accessor AOT tests * [metadata] add ability to inflate wrapper data When we create generic wrappers (or wrappers in a generic class), if the wrapper data needs to refer to a field, method, or parameter type of the definition, that data might need to be inflated if the containing class is inflated (or the generic wrapper method is inflated). Add a new function to opt into inflation: ```c get_marshal_cb ()->mb_inflate_wrapper_data (mb); ``` Add a new function to be called after mono_mb_emit_op (or any other call that calls mono_mb_add_data): ```c mono_mb_emit_op (mb, CEE_LDFLDA, field); mono_mb_set_wrapper_data_kind (mb, MONO_MB_ILGEN_WRAPPER_DATA_FIELD); ``` Note: mono_mb_set_wrapper_data_kind asserts if you haven't called mb_inflate_wrapper_data. TODO: add more wrapper data kinds for MonoMethod* and MonoClass* etc * [marshal] refactor unsafe accessor; opt into inflate_wrapper_data Try to separate the ideas of "the call to the UnsafeAccessor method was inflated, so we need to inflate the wrapper" and "the UnsafeAccessor method is a generic method definition, so the wrapper should be a generic method definition, too" * inflate MonoMethod wrapper data; impl ctor generic unsafe accessors * fix windows build * [aot] handle case of partial generic sharing for unsafe accessor In partial generic sharing, a call to an instance like `Foo<int>` is replaced by `Foo<T_INT>` where T is constrained to `int` and enums that use `int` as a base type. In that case the AOT compiler will emit the unsafe accessor wrapper instantiated with `T_INT`. So in the AOT lookup we have to find calls to `UnsafeAccessor<int>` and replace them with calls to `(wrapper) UnsafeAccessor<T_INT>` to match what the AOT compiler is doing * [aot] for unsafe accessor wrappers with no name, record a length 0 This is needed because for inflated unsafe accessors we write the inflated bit after the name. So if we're accessing a constructor and we didn't record a name in the AOT image, we would erroneously read the inflated bit as the name length. * [aot-runtime] try to fix gsharedvt lookup * better comments; remove fied FIXMEs * update HelloWorld sample to support either normal AOT or FullAOT * rename helper methods * apply suggestions from code review * DRY. compute inflate_generic_data in one place * Just do one loop for inflating generic wrapper data * better comments * DRY. move common AOT code to mini.c
MonoClassField*
directly and then emit a fake token that is just the index of theMonoClassField*
in theMonoMethodWrapper:method_data
array). The pointer data associated with a wrapper is normally just consumed verbatim. But if the wrapper is part of a generic class, or if the wrapper is a generic method, the wrapper data might have generic parameters (for example it might be a MonoClassField forMyList<T>
instead ofMyList<string>
). Add support for tagging the data with its kind and inflating it if the wrapper method is inflated.Contributes to #99830, #89439
NOT DONE