Skip to content

JIT: Avoid keeping BYREFs live across async calls when boxing - #121621

Merged
jakobbotsch merged 3 commits into
dotnet:mainfrom
jakobbotsch:async-byref-fix
Nov 17, 2025
Merged

jakobbotsch merged 3 commits into
dotnet:mainfrom
jakobbotsch:async-byref-fix

Conversation

@jakobbotsch

@jakobbotsch jakobbotsch commented Nov 14, 2025 •

Copy link
Copy Markdown
Member

When boxing or storing values that may contain async calls in async methods, the
JIT was creating IR where BYREF addresses (computed as base + offset) were
evaluated before the async call and kept live across await points. This violated
the async transformation's invariant that BYREFs cannot be live across await
points, causing an assert.

Updated impImportAndPushBox() and STSFLD handling to use the
gtTreeContainsAsyncCall helper to spill values containing async calls before
computing BYREF destinations, ensuring BYREFs are never kept live across await
points.

When boxing or storing values that may contain async calls in async
methods, the JIT was creating IR where BYREF addresses (computed as
base + offset) were evaluated before the async call and kept live
across await points. This violated the async transformation's invariant
that BYREFs cannot be live across await points, causing an assert:
!dsc->TypeIs(TYP_BYREF) in async.cpp:1085.

The fix introduces gtContainsAsyncCall(), a helper that efficiently
checks if a tree contains an async call by using a GenTreeVisitor that
leverages GTF_CALL to skip subtrees without calls. The helper includes
the compIsAsync() check internally.

Updated impImportAndPushBox() and STSFLD handling to use this helper
to spill values containing async calls before computing BYREF
destinations, ensuring BYREFs are never kept live across await points.
@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 Nov 14, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Comment thread src/coreclr/jit/importercalls.cpp Resolved
@jakobbotsch

Copy link
Copy Markdown
Member Author

cc @dotnet/jit-contrib PTAL @EgorBo

Copilot AI 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 fixes an issue where the JIT was keeping BYREF addresses live across async call await points, violating an async transformation invariant. The fix introduces spilling logic to ensure values containing async calls are materialized before computing BYREF destinations.

Key Changes:

  • Introduced gtContainsAsyncCall() helper to detect async calls in expression trees
  • Updated impImportAndPushBox() to spill values with async calls before boxing
  • Improved STSFLD handling to spill only when the stored value contains an async call (more precise than before)

Reviewed Changes

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

File Description
src/coreclr/jit/gentree.cpp Adds new gtContainsAsyncCall() helper function using GenTreeVisitor
src/coreclr/jit/compiler.h Declares the new gtContainsAsyncCall() function
src/coreclr/jit/importer.cpp Uses new helper in boxing and STSFLD to spill values with async calls before computing BYREF destinations
src/coreclr/jit/importercalls.cpp Updates debug string for temp variable (minor cleanup)

Comment thread src/coreclr/jit/importer.cpp Resolved
Comment thread src/coreclr/jit/gentree.cpp Resolved
Comment thread src/coreclr/jit/compiler.h Resolved
Comment thread src/coreclr/jit/importer.cpp Resolved
Comment thread src/coreclr/jit/gentree.cpp Resolved
@jakobbotsch
jakobbotsch merged commit f74e84a into dotnet:main Nov 17, 2025
@jakobbotsch
jakobbotsch deleted the async-byref-fix branch November 17, 2025 12:08
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 18, 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 runtime-async

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants