Skip to content

Conversation

jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Jul 16, 2025

This implements the remaining handling: saving and restoring Thread.CurrentThread._synchronizationContext around runtime async calls that finish synchronously. Lots of trickiness in representing this in the JIT. We introduce a new "suspended indicator" local that is defined by async calls and that the front end uses when it expands IR that restores the field.

Plenty of optimization opportunities remaining, including a simple jump threading optimization we should be able to do to make the suspension indicator variable disappear in common cases (all cases except when resuming with an exception).

Note: it might be beneficial to read my comment below to get a better understanding on what the IR being expanded by the JIT corresponds to.

@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 Jul 16, 2025
Copy link
Contributor

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

@jakobbotsch jakobbotsch reopened this Jul 18, 2025
@jakobbotsch jakobbotsch marked this pull request as ready for review July 18, 2025 12:55
@jakobbotsch
Copy link
Member Author

jakobbotsch commented Jul 18, 2025

cc @dotnet/jit-contrib PTAL @AndyAyersMS

This implements my last known piece of JIT-side compatibility/correctness for runtime async. With this I believe we should be fully compatible with async 1, but of course many pieces of optimizations are missing (and also there may be bugs).
I have run Fuzzlyn over this for many hours so it is tested for asserts to some extent, but Fuzzlyn's correctness coverage of SynchronizationContext and TaskScheduler is not good, so the actual compatibility with async1 is not as well tested. The should match the mental model I have built of SynchronizationContext over the past weeks/months. Maybe in the next few weeks I can introduce better SynchronizationContext testing to Fuzzlyn so I can increase the confidence in the compatibility.

cc @VSadov @agocke too. @agocke this will probably regress your benchmark somewhat due to the added ceremony around SynchronizationContext. Some of that can be optimized out in the future (e.g. see my comments above), while some will probably not be realistic/possible to remove.

@jakobbotsch jakobbotsch requested a review from AndyAyersMS July 18, 2025 13:03
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.

LGTM. Left a few notes that can be handled in a follow-up.

@jakobbotsch
Copy link
Member Author

/ba-g Infra timeout, succeeded earlier

@jakobbotsch
Copy link
Member Author

@VSadov Any other feedback here?

Copy link
Member

@VSadov VSadov left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@jakobbotsch jakobbotsch merged commit 167d862 into dotnet:main Jul 24, 2025
104 of 106 checks passed
@jakobbotsch jakobbotsch deleted the restore-sync-context-field branch July 24, 2025 11:41
@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 2025
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants