Skip to content

Conversation

@MichalStrehovsky
Copy link
Member

This makes virtual methods work with runtime async. Depends on #121438.

Runtime async methods can be virtually called two ways: as Task-returning, or as runtime-async. We can even end up in situation where Task-returning non-runtime-async virtual methods get called as runtime-async.

The easiest way to solve this is to give each Task-returning method two separate virtual slots. We still track the slot use so unused slots will not get generated.

The VirtualMethodAlgorithm abstraction that we added for universal shared generics comes in handy because it lets us centralize where all of this work happens. As a result, we don't need to teach pretty much any node dealing with virtuals about runtime async, it just falls out.

This doesn't make generic virtual methods yet, those need more fixes.

Cc @dotnet/ilc-contrib

This makes virtual methods work with runtime async. Depends on dotnet#121438.

Runtime async methods can be virtually called two ways: as Task-returning, or as runtime-async. We can even end up in situation where Task-returning non-runtime-async virtual methods get called as runtime-async.

The easiest way to solve this is to give each Task-returning method two separate virtual slots. We still track the slot use so unused slots will not get generated.

The `VirtualMethodAlgorithm` abstraction that we added for universal shared generics comes in handy because it lets us centralize where all of this work happens. As a result, we don't need to teach pretty much any node dealing with virtuals about runtime async, it just falls out.

This doesn't make generic virtual methods yet, those need more fixes.
@dotnet-policy-service
Copy link
Contributor

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

Copy link
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 enhances the compiler type system context to support async-aware virtual method resolution. The key changes introduce a custom AsyncAwareVirtualMethodResolutionAlgorithm that handles async variant methods during virtual method dispatch, replacing the basic MetadataVirtualMethodAlgorithm in the AOT compilation path while maintaining the simple algorithm for ReadyToRun.

Key Changes:

  • Implements async-aware virtual method resolution algorithm that properly handles async variant methods during virtual dispatch
  • Adds support for EmitAsyncMethodThunk to generate thunks for async calling conventions
  • Fixes the IsAsyncVariant extension method to properly check method definitions

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ReadyToRunCompilerContext.cs Adds MetadataVirtualMethodAlgorithm field to ReadyToRun compiler context partial class
CompilerTypeSystemContext.Aot.cs Adds AsyncAwareVirtualMethodResolutionAlgorithm field initialization for AOT scenarios
CompilerTypeSystemContext.cs Removes the base MetadataVirtualMethodAlgorithm field (moved to specific contexts)
CompilerTypeSystemContext.Async.cs Implements AsyncAwareVirtualMethodResolutionAlgorithm and GetTargetOfAsyncVariantMethod helper
AsyncThunks.cs Adds EmitAsyncMethodThunk method to generate async thunks (currently throws NotSupportedException)
NativeAotILProvider.cs Replaces TODO with call to EmitAsyncMethodThunk for async variant implementations
AsyncMethodVariant.cs Fixes IsAsyncVariant to check method definition instead of instance type

@jkotas
Copy link
Member

jkotas commented Nov 8, 2025

Failures look related

2025-11-07T23:01:28.6641071Z     System.NullReferenceException: Object reference not set to an instance of an object.
2025-11-07T23:01:28.6642539Z        at ILCompiler.DependencyAnalysis.EETypeNode.GetConditionalStaticDependencies(NodeFactory factory) in /_/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs:line 383

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM!

@MichalStrehovsky
Copy link
Member Author

/ba-g android timeouts

@MichalStrehovsky MichalStrehovsky merged commit 9155f26 into dotnet:main Nov 8, 2025
83 of 97 checks passed
@MichalStrehovsky MichalStrehovsky deleted the asyncvirtual branch November 8, 2025 23:40
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.

3 participants