-
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
NativeAOT: Dependency analysis support for generic method reflection #81204
Comments
It could but we haven't put the effort into it because I didn't think this pattern would be common. We would need to know the exact method and the exact instantiation arguments. Whenever I see this pattern, the instantiation arguments are usually statically unknown. If everything is statically known, one is usually better off e.g. constructing a delegate. Do you have a real world example of this? |
efcore is using this pattern heavily to get generic method. |
Do you have an example? I did a quick search for |
An example: https://github.com/npgsql/efcore.pg/pull/2028/files#r725139551 |
That one doesn't seem to be calling In general if the exact generic method is known, it's probably more efficient to get a delegate to it and if one really needs a |
Example of where this would be useful: internal static class ResultsOfTHelper
{
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "MakeGenericType is safe because the target can be statically analyzed during compile.")]
public static void PopulateMetadataIfTargetIsIEndpointMetadataProvider<TTarget>(MethodInfo method, EndpointBuilder builder)
{
if (typeof(IEndpointMetadataProvider).IsAssignableFrom(typeof(TTarget)))
{
var methodInfo = typeof(MetadataPopulator<>).MakeGenericType(typeof(TTarget)).GetMethod("PopulateMetadata")!;
methodInfo.Invoke(null, new object[] { method, builder });
}
}
internal static class MetadataPopulator<TTarget> where TTarget : IEndpointMetadataProvider
{
public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
{
TTarget.PopulateMetadata(method, builder);
}
}
} It currently fails when TTarget is a value type. ASP.NET Core issue to use future improvement: dotnet/aspnetcore#46267 |
@MichalStrehovsky Even everything is statically known, we may still have to use MakeGenericType/MakeGenericMethod due to some generic constraints cannot be satisfied at the compile time but only at the runtime. |
Yup, that's why I brought this over from runtimelab. The linked discussion in the aspnetcore repo is about bridging constraints |
We don't have a good spot to do this in the compiler. Dataflow analysis runs on top of uninstantiated IL so we can't do this there. This would ideally be done in RyuJIT (intrinsic expansion of Another closest spot where we could do this is the constant propagation (that rewrites IL) - this would be a single spot on the managed compiler side and it would not require RyuJIT work. However, we don't have facilities to easily do it here. The result would not be great. I'm moving this to 9.0. This would be better done if we lay some foundational groundwork first. |
Contributes to dotnet#81204. With this, we can handle `typeof(Foo<>).MakeGenericType(typeof(T))` (and `MakeGenericMethod`), including in shared generic code. The dataflow analysis change should be straightforward (we just look if it's a known array, with all known elements). I scoped this down to "exactly one array" and "exactly one possible value in the element" because the objective is constraint bridging code. We could extend to supporting multiple values, but I don't see a need. If we know what the values are, we elide generating a warning. The support for this in the rest of the compiler is a bit annoying due to generics. We only run dataflow on uninstantiated code. So we have a dependency node representing the dataflow analysis. This node obviously cannot know what are all the possible instantiations we saw, so it cannot directly report things about instantiated generic code. Three possible options to solve this: * Re-run dataflow analysis on instantiated code. I didn't feel great about doing that. * Introduce a new node `SpecializedDataflowAnalyzedMethodNode` that would be injected as a conditional dependency of all scanned/compiled method bodies, conditioned on `DataflowAnalyzedMethodNode` being present. This would work, but to obtain a `DataflowAnalyzedMethod` we need the `MethodIL` of the definition (not the `MethodDesc`) and that started to look sketchy. * Use dynamic dependencies. We only use these for GVM analysis right now. They give a node the ability to receive callbacks when new nodes are added to the graph. These nodes need to declare they are "interesting" to dynamic dependency analysis. It's not great, but it works. I went with option 3. I'm not making this resolve the above issue. We'd ideally want to implement this in the analyzer too. I had a quick look at that and it looks like we need to write a brand new `DiagnosticAnalyzer` similar to the `DynamicallyAccessedMembersAnalyzer`.
Contributes to #81204. With this, we can handle `typeof(Foo<>).MakeGenericType(typeof(T))` (and `MakeGenericMethod`), including in shared generic code. The dataflow analysis change should be straightforward (we just look if it's a known array, with all known elements). I scoped this down to "exactly one array" and "exactly one possible value in the element" because the objective is constraint bridging code. We could extend to supporting multiple values, but I don't see a need. If we know what the values are, we elide generating a warning. The support for this in the rest of the compiler is a bit annoying due to generics. We only run dataflow on uninstantiated code. So we have a dependency node representing the dataflow analysis. This node obviously cannot know what are all the possible instantiations we saw, so it cannot directly report things about instantiated generic code. Three possible options to solve this: * Re-run dataflow analysis on instantiated code. I didn't feel great about doing that. * Introduce a new node `SpecializedDataflowAnalyzedMethodNode` that would be injected as a conditional dependency of all scanned/compiled method bodies, conditioned on `DataflowAnalyzedMethodNode` being present. This would work, but to obtain a `DataflowAnalyzedMethod` we need the `MethodIL` of the definition (not the `MethodDesc`) and that started to look sketchy. * Use dynamic dependencies. We only use these for GVM analysis right now. They give a node the ability to receive callbacks when new nodes are added to the graph. These nodes need to declare they are "interesting" to dynamic dependency analysis. It's not great, but it works. I went with option 3. I'm not making this resolve the above issue. We'd ideally want to implement this in the analyzer too. I had a quick look at that and it looks like we need to write a brand new `DiagnosticAnalyzer` similar to the `DynamicallyAccessedMembersAnalyzer`.
Follow up to dotnet#99037. Resolves dotnet#81204. When `MakeGenericXXX` API is used in the "bridging constraints" pattern and all types/members involved are statically known, don't emit warning from the Roslyn analyzer since ILC will do the right thing and ensure this works.
…t#100858) Follow up to dotnet#99037. Resolves dotnet#81204. When `MakeGenericXXX` API is used in the "bridging constraints" pattern and all types/members involved are statically known, don't emit warning from the Roslyn analyzer since ILC will do the right thing and ensure this works.
…t#100858) Follow up to dotnet#99037. Resolves dotnet#81204. When `MakeGenericXXX` API is used in the "bridging constraints" pattern and all types/members involved are statically known, don't emit warning from the Roslyn analyzer since ILC will do the right thing and ensure this works.
I hope ILCompiler dependency analysis can take generic pattern into account so that we don't need
rd.xml
for this case any longer:Current behavior without a
rd.xml
:The text was updated successfully, but these errors were encountered: