Skip to content

Update EmitTaskReturningThunk in Native AOT - #121435

Merged
eduardo-vp merged 15 commits into
dotnet:mainfrom
eduardo-vp:emitTaskReturningThunk-native-aot
Nov 13, 2025
Merged

Update EmitTaskReturningThunk in Native AOT#121435
eduardo-vp merged 15 commits into
dotnet:mainfrom
eduardo-vp:emitTaskReturningThunk-native-aot

Conversation

@eduardo-vp

@eduardo-vpeduardo-vp commented Nov 7, 2025

Copy link
Copy Markdown
Member

Updated EmitTaskReturningThunk in Native AOT to match EmitTaskReturningThunk in CoreCLR.

@MichalStrehovsky

MichalStrehovsky commented Nov 7, 2025

Copy link
Copy Markdown
Member

The IL generated in CoreCLR calls methods that are not available in Native AOT, need to figure out how to handle those scenarios.

I added AsyncHelpers.CoreCLR.cs to native AOT corelib in #121398 so we should have those now. Just do a merge from main!

Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
@MichalStrehovsky

Copy link
Copy Markdown
Member

Once you have this, you should be able to observe this program (same one I pointed you to, but with added Task.Delay) print two numbers, freeze for a second and then crash with an unhandled exception since we need the resumption thunk to continue.

usingSystem;usingSystem.Threading.Tasks;publicclassAsync2Void{publicstaticvoidMain(){vart=AsyncTestEntryPoint(123,456);t.Wait();Console.WriteLine(t.Result);}privatestaticasyncTask<int>AsyncTestEntryPoint(intx,inty){intresult=awaitOtherAsync(x,y);returnresult;}privatestaticasyncTask<int>OtherAsync(intx,inty){Console.WriteLine(x);Console.WriteLine(y);awaitTask.Delay(1000);returnx+y;}}

eduardo-vpand others added 8 commits November 7, 2025 16:07
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Michal Strehovský <MichalStrehovsky@users.noreply.github.com>
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
@eduardo-vp

eduardo-vp commented Nov 11, 2025

Copy link
Copy Markdown
MemberAuthor

Once you have this, you should be able to observe this program (same one I pointed you to, but with added Task.Delay) print two numbers, freeze for a second and then crash with an unhandled exception since we need the resumption thunk to continue.

Unfortunately the program prints the numbers and crashes but doesn't freeze, I'm looking into that.

@eduardo-vp
eduardo-vp marked this pull request as ready for review November 11, 2025 00:47
CopilotAI review requested due to automatic review settings November 11, 2025 00:47

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

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 updates the EmitTaskReturningThunk method in the Native AOT type system to match the CoreCLR VM implementation. The goal is to generate consistent IL code for async thunk methods across both runtimes.

Key changes:

  • Added support for catch exception regions in the IL emitter infrastructure
  • Implemented full task-returning thunk logic with nested try-catch-finally blocks
  • Added sorting of exception regions to meet ECMA-335 spec requirements
  • Created stub for AsyncCallContinuation intrinsic in NativeAOT

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
FileDescription
src/coreclr/vm/asyncthunks.cppUpdated comments to clarify that the code should match the managed type system implementation
src/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.csAdded support for catch exception regions, updated exception region infrastructure to handle multiple types, and added sorting logic for proper exception region ordering
src/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.csImplemented comprehensive task-returning thunk logic with proper exception handling, generic instantiation support, and control flow matching CoreCLR
src/coreclr/nativeaot/System.Private.CoreLib/src/System/StubHelpers.NativeAot.csAdded AsyncCallContinuation intrinsic stub for NativeAOT
src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csprojAdded new StubHelpers.NativeAot.cs file to the project

Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
@eduardo-vp

Copy link
Copy Markdown
MemberAuthor

Once you have this, you should be able to observe this program (same one I pointed you to, but with added Task.Delay) print two numbers, freeze for a second and then crash with an unhandled exception since we need the resumption thunk to continue.

The program works as expected now.

Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
Comment threadsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncThunks.cs Outdated
@eduardo-vp

Copy link
Copy Markdown
MemberAuthor

/ba-g timeouts

@eduardo-vp
eduardo-vp merged commit f873b2f into dotnet:mainNov 13, 2025
91 of 98 checks passed
@eduardo-vp
eduardo-vp deleted the emitTaskReturningThunk-native-aot branch November 13, 2025 02:52
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Dec 13, 2025
Sign up for freeto 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.

5 participants

@eduardo-vp@MichalStrehovsky@jkotas@VSadov