Skip to content

Conversation

jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Jun 26, 2025

Add support for saving and restoring the ExecutionContext around runtime async calls. Most of this is done in the JIT:

  • For task awaits, ExecutionContext is stored on both synchronous and asynchronous (suspending/resuming) paths. The JIT inserts IR between import and inlining to save the context before each async call, and to restore it after. If the call is inside a try clause, then this requires insertion of a try-finally to ensure the context is restored even if an exception is thrown.
  • For custom awaits, ExecutionContext is saved and restored only in the asynchronous path. Thus the JIT only saves/restores the context as part of the suspend/resume paths.

For the first case above we allow inlining the helpers. The second case above happens too late to rely on normal inlining, so right now it just inserts calls to two helpers. In the future we can consider inlining these manually in the suspend/restore paths.

For runtime async calls we assume that Thread.t_currentThread is always initialized. This initialization happens as part of DispatchContinuations or the Task-returning thunks. So we can avoid calling InitializeCurrentThread when capturing/restoring the contexts. I have introduced a new Thread.CurrentThreadNoInit to access the TLS field without initialization.

@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jun 26, 2025
Copy link
Contributor

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

Retbuf calls are TYP_VOID but still come with RET_EXPRs. Use
`gtReturnType` instead.
@jakobbotsch jakobbotsch marked this pull request as ready for review June 27, 2025 21:58
@jakobbotsch
Copy link
Member Author

cc @dotnet/jit-contrib PTAL @AndyAyersMS

Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

JIT changes LGTM. Left a few comments for your consideration.

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

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI runtime-async

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants