Skip to content

Conversation

@jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Nov 19, 2025

NativeAOT and coreclr have various places where they want to have explicit control over the generic context and async continuation arguments. Introduce JIT intrinsics that allow them to explicitly set what value should be used for these arguments in an upcoming call. Model these arguments as part of the calling convention in the signatures where they are passed. That fixes #118575 for async, and is also necessary to compute proper call stubs for the interpreter.

See #121781 for more context.

NativeAOT and coreclr have various places where they want to have
explicit control over the generic context and async continuation
arguments. Introduce JIT intrinsics that allow them to explicitly set
what value should be used for these arguments in an upcoming call.

Also switch the async resumption stub's call to be properly marked
async, and clean up some JIT side hacks as a result. That fixes dotnet#118575.
@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 19, 2025
@dotnet-policy-service
Copy link
Contributor

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

@jakobbotsch
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jakobbotsch
Copy link
Member Author

I asked @davidwrighton for some help with implementing the same support in the interpreter.

@jakobbotsch jakobbotsch marked this pull request as ready for review November 24, 2025 19:50
Copilot AI review requested due to automatic review settings November 24, 2025 19:50
Copilot finished reviewing on behalf of jakobbotsch November 24, 2025 19:53
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 introduces new JIT intrinsics (SetNextCallGenericContext and SetNextCallAsyncContinuation) to provide explicit control over generic context and async continuation arguments in method calls. The changes eliminate platform-specific conditional compilation and simplify stub generation by modeling these arguments as part of the calling convention signature rather than as explicit parameters.

Key changes:

  • Adds two new intrinsic methods to RuntimeHelpers for setting generic context and async continuation for the next call
  • Refactors stub generation in CoreCLR VM to use the new intrinsics instead of manually inserting arguments
  • Updates the JIT importer to handle the new intrinsics and manage per-call context/continuation state

Reviewed changes

Copilot reviewed 12 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.cs Adds SetNextCallGenericContext and SetNextCallAsyncContinuation intrinsic methods
src/coreclr/vm/prestub.cpp Refactors CreateDerivedTargetSig (renamed) to use intrinsics; updates unboxing and instantiating stub generation to call intrinsics instead of manually inserting parameters
src/coreclr/vm/method.hpp Updates method signature declaration to match rename
src/coreclr/vm/jitinterface.cpp Updates async resumption stub generation to use new intrinsics and removes platform-specific conditional compilation
src/coreclr/vm/corelib.h Defines method metadata entries for the new intrinsics
src/coreclr/vm/callstubgenerator.cpp Fixes indentation in logging code
src/coreclr/jit/namedintrinsiclist.h Adds named intrinsic enum entries for the two new methods
src/coreclr/jit/lower.cpp Updates comments to reflect that NativeAOT also uses the new intrinsic-based approach
src/coreclr/jit/importercalls.cpp Implements intrinsic handling; adds logic to consume stored context/continuation values in call import; restructures generic context resolution flow
src/coreclr/jit/compiler.h Adds fields to track the next call's generic context and async continuation local variables
src/coreclr/interpreter/intrinsics.cpp Adds intrinsic recognition for the interpreter
src/coreclr/interpreter/compiler.h Adds fields to track context/continuation variables in interpreter
src/coreclr/interpreter/compiler.cpp Implements interpreter handling for the new intrinsics
src/coreclr/inc/corhdr.h Reformats calling convention constant definitions

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jakobbotsch
Copy link
Member Author

cc @dotnet/jit-contrib PTAL @EgorBo @jkotas @VSadov

@jkotas
Copy link
Member

jkotas commented Nov 24, 2025

VM changes LGTM - as an interim step.

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.

@jakobbotsch jakobbotsch merged commit 8734e30 into dotnet:main Nov 25, 2025
158 of 160 checks passed
@jakobbotsch jakobbotsch deleted the special-arg-intrinsics branch November 25, 2025 08:45
@jakobbotsch
Copy link
Member Author

I should have asked for a review on the interpreter parts. cc @janvorli @BrzVlad @kg @davidwrighton. Let me know if you have any feedback and I can open a follow up PR to address it.

MichalStrehovsky added a commit that referenced this pull request Nov 26, 2025
Takes advantage of the new functionality added in #121799.
radekdoulik added a commit to radekdoulik/runtime that referenced this pull request Dec 1, 2025
Fix regression introduced in dotnet#121799

Force intrinsic expansion for new runtime helpers in the interpreter.

Added the new methods and also
NI_System_StubHelpers_NextCallReturnAddress, which has also `//
Unconditionally expanded intrinsic` comment.

This was happening on wasm and possibly on other platforms too,
resulting in unhandled exception. Possibly only happening on platforms
using generic instantiation IL stubs.

```
Unhandled exception. System.Diagnostics.UnreachableException: The program executed an instruction that was thought to be unreachable.
   at System.Runtime.CompilerServices.RuntimeHelpers.SetNextCallGenericContext(Void* value) in /Users/rodo/git/runtime-clean/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.cs:line 186
   at InterpreterTest.TestVirtual() in /Users/rodo/git/runtime-clean/src/tests/JIT/interpreter/Interpreter.cs:line 1960
   at InterpreterTest.RunInterpreterTests() in /Users/rodo/git/runtime-clean/src/tests/JIT/interpreter/Interpreter.cs:line 896
   at InterpreterTest.Main(String[] args) in /Users/rodo/git/runtime-clean/src/tests/JIT/interpreter/Interpreter.cs:line 731
Aborted(native code called abort())
```
radekdoulik added a commit that referenced this pull request Dec 1, 2025
Fix regression introduced in #121799

Force intrinsic expansion for new runtime helpers in the interpreter.

Added the new methods and also
`NI_System_StubHelpers_NextCallReturnAddress`, which has also `//
Unconditionally expanded intrinsic` comment.

This was happening on wasm and possibly on other platforms too,
resulting in unhandled exception. Possibly only on platforms, which use
generic instantiation IL stubs.

```
Unhandled exception. System.Diagnostics.UnreachableException: The program executed an instruction that was thought to be unreachable.
   at System.Runtime.CompilerServices.RuntimeHelpers.SetNextCallGenericContext(Void* value) in /Users/rodo/git/runtime-clean/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.cs:line 186
   at InterpreterTest.TestVirtual() in /Users/rodo/git/runtime-clean/src/tests/JIT/interpreter/Interpreter.cs:line 1960
   at InterpreterTest.RunInterpreterTests() in /Users/rodo/git/runtime-clean/src/tests/JIT/interpreter/Interpreter.cs:line 896
   at InterpreterTest.Main(String[] args) in /Users/rodo/git/runtime-clean/src/tests/JIT/interpreter/Interpreter.cs:line 731
Aborted(native code called abort())
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

Runtime async should use async call conv for calli in async resumption stubs

5 participants