Skip to content

Expand discovery of generic methods on r2r#127510

Draft
BrzVlad wants to merge 3 commits intodotnet:mainfrom
BrzVlad:fix-r2r-generics-pr2
Draft

Expand discovery of generic methods on r2r#127510
BrzVlad wants to merge 3 commits intodotnet:mainfrom
BrzVlad:fix-r2r-generics-pr2

Conversation

@BrzVlad
Copy link
Copy Markdown
Member

@BrzVlad BrzVlad commented Apr 28, 2026

Called methods are obtained from MethodFixupSignature and are stored into ReadyToRunVirtualMethodDependenciesNode. These will be nodes that HasDynamicDependencies so the dependency analysis will do additional iterations for these nodes on the newly added nodes that are InterestingForDynamicDependencyAnalysis. These will be InheritedVirtualMethodsNode added as part of TypeFixupSignature. They will represent types used by the application. We resolve the target method for calling the virtual/interface method on the existing types. This attempts to reuse code that NativeAOT also used for this purpose. If we are able to obtain a new method, we include it in the compilation.

BrzVlad added 3 commits April 25, 2026 17:20
When dependency analysis reaches a virtual invocation, we add this method to the dynamic dependency analysis, so that we end up including all possible implementations on other types.
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

This PR expands ReadyToRun (R2R) compilation discovery for generic virtual/interface calls by adding dynamic dependency analysis that resolves additional concrete implementations as new application-used types are discovered in the dependency graph.

Changes:

  • Added shared TypeExtensions helpers to resolve canonical interface implementations and class virtual overrides on concrete types.
  • Introduced two new R2R dependency-analysis nodes (InheritedVirtualMethodsNode, ReadyToRunVirtualMethodDependenciesNode) and wired them into the R2R node factory and build.
  • Hooked type and method fixup signatures to (a) mark relevant concrete types and (b) track virtual callsites that should trigger dynamic discovery; refactored NativeAOT GVMDependenciesNode to reuse the new resolution helpers.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/coreclr/tools/Common/Compiler/TypeExtensions.cs Adds reusable helpers for resolving canonical interface implementations and class virtual overrides on concrete types.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/ILCompiler.ReadyToRun.csproj Includes the new dependency-analysis node source files in the R2R build.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunVirtualMethodDependenciesNode.cs New dynamic-dependency node that discovers and adds compiled-method dependencies for resolved implementations as types are discovered.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunCodegenNodeFactory.cs Adds node caches/factory methods for the new marker and virtual-call dependency nodes.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/TypeFixupSignature.cs Adds a phase-0 marker dependency so discovered concrete types participate in later virtual dispatch implementation discovery.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/MethodFixupSignature.cs Adds a virtual-call dependency node for eligible generic virtual/interface VirtualEntry fixups (with a guard to skip async variants).
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/InheritedVirtualMethodsNode.cs New “interesting” marker node representing a concrete type for virtual dispatch discovery.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/GVMDependenciesNode.cs Refactors interface/class override discovery to use the new TypeExtensions helpers.

Comment on lines +76 to +84
// FIXME Including async methods can lead to crash when compiling Async variant: [S.P.CoreLib] System.IO.TextWriter + SyncTextWriter.DisposeAsync()
// Crash is assertion in constructStringLiteral that gets called when compiling an IL stub.
//
// For generic virtual method calls, create a virtual dependency node that will
// dynamically discover implementations on types as they are added to the graph.
if (_fixupKind == ReadyToRunFixupKind.VirtualEntry &&
Method.IsVirtual &&
!Method.IsFinal &&
!Method.IsAsyncVariant() &&
Comment on lines +96 to +101
hasNonCanonArgs = true;
}
foreach (TypeDesc arg in canonMethod.OwningType.Instantiation)
{
if (arg != context.CanonType)
hasNonCanonArgs = true;
Comment on lines +50 to +56
List<CombinedDependencyListEntry> dynamicDependencies = new List<CombinedDependencyListEntry>();

bool methodIsShared = _method.IsSharedByGenericInstantiations;

for (int i = firstNode; i < markedNodes.Count; i++)
{
DependencyNodeCore<NodeFactory> entry = markedNodes[i];
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.

2 participants