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

Handle MakeGeneric APIs in dataflow #99037

Merged
merged 6 commits into from
Mar 6, 2024

Conversation

MichalStrehovsky
Copy link
Member

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.

Cc @dotnet/ilc-contrib

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`.
@ghost
Copy link

ghost commented Feb 28, 2024

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

Cc @dotnet/ilc-contrib

Author: MichalStrehovsky
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

@MichalStrehovsky MichalStrehovsky merged commit 78452bf into dotnet:main Mar 6, 2024
143 of 145 checks passed
@MichalStrehovsky MichalStrehovsky deleted the makegenericflow branch March 6, 2024 22:40
@MichalStrehovsky
Copy link
Member Author

@yowl @SingleAccretion this will need a matching change in nativeAOT-LLVM - just match the one-line diff in ScannedMethodNode/MethodCodeNode in the node that represents compiled method bodies.

The failure mode you'll see without the matching change is runtime failure in the new tests that do MakeGenericType/Method.

If you wanted to, you could just proactively make the change now, I don't think it'll hurt anything.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants