Skip to content

Conversation

@jtschuster
Copy link
Member

@jtschuster jtschuster commented Nov 16, 2025

Async thunk IL references methods and types that may not have MethodRef or TypeRef tokens in the assembly being crossgen'ed. There are some assertions to validate that any references in "faux il" version with the assembly, which isn't true for these types and methods. To work around this, we can inject these references into the mutable module, a synthetic module added to the assembly for things like this. When trying to compile an async thunk method, we check to see if all the required methods are already in the mutable module. If not, we note which methods need to be added during the single-threaded execution, and queue the method for recompilation next iteration.

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 adds infrastructure to insert references for known types and methods required by async thunks into the mutable module. This is preparatory work for supporting async methods in ReadyToRun compilation, though async method compilation remains disabled due to unresolved issues with AsyncMethodVariant.GetTypicalMethodDefinition.

Key Changes

  • Added AddingReferencesToR2RKnownTypesAndMethods flag to MutableModule to allow adding references to known async-related types/methods
  • Implemented InitializeKnownMethodsAndTypes() to populate known async-related types and methods from System.Private.CoreLib into the mutable module
  • Updated assertion logic to handle references to these known types/methods that exist outside the version bubble
  • Added early exit for async variant methods in compilation (they remain unsupported for now)
  • Refactored AsyncThunks.cs to declare exception type earlier for use in catch region definition

Reviewed Changes

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

Show a summary per file
File Description
src/coreclr/tools/aot/ILCompiler.ReadyToRun/TypeSystem/Mutable/MutableModule.cs Added AddingReferencesToR2RKnownTypesAndMethods flag and updated assertion to allow references during async type/method initialization
src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs Added early exit for async variants and updated token resolution logic to handle known mutable module types/methods
src/coreclr/tools/aot/ILCompiler.ReadyToRun/ILCompiler.ReadyToRun.csproj Added project references to async stub files (AsyncThunks.cs, AsyncResumptionStub.cs and related files)
src/coreclr/tools/aot/ILCompiler.ReadyToRun/IL/ReadyToRunILProvider.cs Added resolver parameter to InitManifestMutableModule and removed assertion that blocked async variants
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunCodegenNodeFactory.cs Updated InitManifestMutableModule call to pass the Resolver parameter
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs Implemented InitializeKnownMethodsAndTypes() to populate known async types/methods and added helper methods to check if types/methods are in the known set
src/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Moved exception type declaration earlier to be accessible for catch region creation

@jkotas
Copy link
Member

jkotas commented Nov 16, 2025

This is effectively creating an alternative scheme for encoding ReadyToRunHelper (

enum ReadyToRunHelper
).

I expect that we will use it for encoding higher-level helpers outside async too. For example, it would be fairly natural to use it to encode READYTORUN_HELPER_GetCurrentManagedThreadId that is an alias for Environment.CurrentManagedThreadId method.

@jtschuster jtschuster linked an issue Nov 18, 2025 that may be closed by this pull request
@jkotas
Copy link
Member

jkotas commented Nov 25, 2025

Just curious - what made you switch from the metadata encoding to the encoding via ReadyToRunHelper enum?

@jtschuster
Copy link
Member Author

Just curious - what made you switch from the metadata encoding to the encoding via ReadyToRunHelper enum?

Maybe I misunderstood, but I thought you were recommending this in your other comments.

Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

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

This looks good to me modulo the questions, but ideally someone who knows about GetModuleTokenForMethod should have a look too.

@jtschuster jtschuster merged commit 72cec96 into dotnet:main Dec 16, 2025
98 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 16, 2026
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.

AsyncHelpers used in async thunks may not have references in the ReadyToRun version bubble Fix references in generated IL with mutable module.

4 participants