Skip to content

Fix dataflow analysis for MakeGenericType/MakeGenericMethod in local methods#127152

Open
Copilot wants to merge 2 commits intomainfrom
copilot/fix-dataflow-analysis-makegeneric
Open

Fix dataflow analysis for MakeGenericType/MakeGenericMethod in local methods#127152
Copilot wants to merge 2 commits intomainfrom
copilot/fix-dataflow-analysis-makegeneric

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 20, 2026

Description

Dataflow analysis associates MakeGenericTypeSite/MakeGenericMethodSite runtime dependencies with the parent user method, but SearchDynamicDependencies only matches compiled method bodies against that parent. When the MakeGenericType call lives in a compiler-generated local method, the local method's GetTypicalMethodDefinition() differs from the parent, so the dependency is never instantiated.

MakeGeneric<Atom>();

// This local method's MakeGenericTypeSite was never matched in SearchDynamicDependencies
static object MakeGeneric<T>() => Activator.CreateInstance(typeof(Gen<>).MakeGenericType(typeof(T)));

class Gen<T>;
struct Atom;

Fix: Track which method each runtime dependency originated from and match against it:

  • ReflectionMarker.RuntimeDeterminedDependencies now stores (MethodDesc OwningMethod, INodeWithRuntimeDeterminedDependencies Dependency) tuples
  • HandleCallAction tags each MakeGenericTypeSite/MakeGenericMethodSite with _callingMethod
  • DataflowAnalyzedMethodNode.SearchDynamicDependencies matches each marked method body's typical definition against the dependency's owning method, rather than only the analyzed parent method

Added regression test TestMakeGenericDataflowInLocalMethod covering both MakeGenericType and MakeGenericMethod from static local methods.

Copilot AI self-assigned this Apr 20, 2026
Copilot AI review requested due to automatic review settings April 20, 2026 12:59
Copilot AI review requested due to automatic review settings April 20, 2026 12:59
Copilot AI linked an issue Apr 20, 2026 that may be closed by this pull request
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 20, 2026
…methods

Associate each runtime-determined dependency with the method it
originates from (the actual IL method being scanned), rather than
treating all dependencies as belonging to the parent user method.

In SearchDynamicDependencies, match marked method bodies against
the dependency's owning method instead of only the analyzed (parent)
method. This ensures that when a local method like MakeGeneric<Atom>
is compiled, its MakeGenericTypeSite dependencies are correctly
instantiated with the local method's generic arguments.

Fixes #115552

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/81f1c59a-c2ca-4ab8-ae7d-e8edded14d1c

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 20, 2026 13:30
Copilot AI changed the title [WIP] Fix dataflow analysis for MakeGeneric use in local methods Fix dataflow analysis for MakeGenericType/MakeGenericMethod in local methods Apr 20, 2026
Copilot AI requested a review from MichalStrehovsky April 20, 2026 13:34
@github-actions github-actions bot added area-NativeAOT-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Apr 20, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes NativeAOT dataflow/runtime-dependency instantiation for MakeGenericType/MakeGenericMethod when the intrinsic call occurs in compiler-generated nested functions (e.g., local methods), by tracking which method a runtime dependency originated from and matching specializations against that method.

Changes:

  • Track runtime-determined dependencies as (OwningMethod, Dependency) pairs and record the calling method when creating MakeGenericTypeSite/MakeGenericMethodSite.
  • Update dynamic dependency instantiation to match against each marked method body’s typical definition (including compiler-generated nested functions).
  • Add a regression smoke test covering MakeGenericType and MakeGenericMethod from static local methods.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/tests/nativeaot/SmokeTests/TrimmingBehaviors/Dataflow.cs Adds regression test exercising MakeGenericType/MakeGenericMethod inside static local methods.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DataflowAnalyzedMethodNode.cs Matches and instantiates runtime deps based on recorded owning method rather than only the parent analyzed method.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs Updates scan API to return owning-method-tagged runtime dependencies.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMarker.cs Stores runtime-determined dependencies with their owning method.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/HandleCallAction.cs Tags MakeGenericTypeSite/MakeGenericMethodSite runtime deps with the calling method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dataflow analysis doesn't predict MakeGeneric use in local methods

3 participants