Skip to content

Commit 4db3ed1

Browse files
authored
Merge branch 'main' into x86_bigmul
2 parents 70f2ad5 + aa2d370 commit 4db3ed1

37 files changed

Lines changed: 4552 additions & 260 deletions

File tree

docs/design/coreclr/botr/runtime-async-codegen.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The general responsibilities of the runtime-async code generator
1212

1313
2. Allow the async thunk logic to work.
1414

15-
3. Generate Async Debug info (Not yet described in this document)f
15+
3. Generate Async Debug info (Not yet described in this document)
1616

1717

1818

@@ -37,7 +37,7 @@ call <Await> One of the functions which matches NI_System_Runtime_Compiler
3737

3838
A search for this sequence is done if Method is known to be async.
3939

40-
The dispatch to async functions save the `ExecutionContext` on suspension and restore it on resumption via `AsyncHelpers.CaptureExecutionContext` and `AsyncHelpers.RestoreExecutionContext` respectively
40+
The dispatch to async functions saves the `ExecutionContext` on suspension to be restored before resumption by `DispatchContinuations`.
4141

4242
If PREFIX_TASK_AWAIT_CONTINUE_ON_CAPTURED_CONTEXT, then continuation mode shall be ContinuationContextHandling::ContinueOnCapturedContext otherwise ContinuationContextHandling::ContinueOnThreadPool.
4343

@@ -59,9 +59,21 @@ The dispatch to these functions will save and restore the execution context only
5959

6060
When encountered, triggers the function to suspend immediately, and return the passed in Continuation.
6161

62+
# Accessing known continuation fields
63+
64+
The continuation flags encode how to access several well-known fields if they are present in the continuation.
65+
- The execution context
66+
- The continuation context
67+
- The exception object
68+
- The return value
69+
70+
Each field has a pair of (first bit, number of bits) used to indicate where its details are encoded in the flags.
71+
When the field is present the index is non-zero and the offset is computed as (DataStart + (index - 1) * PointerSize).
72+
If a field is not present the index is zero.
73+
6274
# Saving and restoring of contexts
6375

64-
Capture the execution context before the suspension, and when the function resumes, call `AsyncHelpers.RestoreExecutionContext`. The context should be stored into the Continuation. The context may be captured by calling `AsyncHelpers.CaptureExecutionContext`.
76+
Capture the execution context before the suspension. The context should be stored into the Continuation and its offset encoded via the scheme above. The context may be captured by calling `AsyncHelpers.CaptureExecutionContext`.
6577

6678
# ABI for async function handling
6779

@@ -87,8 +99,8 @@ if (continuation != NULL)
8799
// Resumption point
88100
89101
// Copy values out of continuation (including captured sync context and execution context locals)
90-
// If the continuation may have an exception, check to see if its there, and if it is, throw it. Do this if CORINFO\_CONTINUATION\_HAS\_EXCEPTION is set.
91-
// If the continuation has a return value, copy it out of the continuation. (CORINFO\_CONTINUATION\_HAS\_RESULT is set)
102+
// If the continuation may have an exception, check to see if its there, and if it is, throw it. Do this if the flags indicate an exception object is present.
103+
// If the continuation has a return value, copy it out of the continuation. Do this if the flags indicate a result is present.
92104
}
93105
```
94106

@@ -109,16 +121,16 @@ This only applies to calls which where ContinuationContextHandling is not Contin
109121

110122
If set to ContinuationContextHandling::ContinueOnCapturedContext
111123

112-
- The Continuation shall have an allocated data member for the captured context, and the CORINFO_CONTINUATION_HAS_CONTINUATION_CONTEXT flag shall be set on the continuation.
124+
- The Continuation shall have an allocated data member for the captured context, and its offset is encoded in the flags based on the scheme above.
113125

114-
- The Continuation will store the captured synchronization context. This is done by calling `AsyncHelpers.CaptureContinuationContext(ref newContinuation.ContinuationContext, ref newContinuation.Flags)` while filling in the `Continuation`.
126+
- The Continuation will store the captured synchronization context. This can be done by calling `AsyncHelpers.CaptureContinuationContext(ref newContinuation.ContinuationContext, ref newContinuation.Flags)` while filling in the `Continuation`.
115127

116128
If set to ContinuationContextHandling::ContinueOnThreadPool
117129
- The Continuation shall have the CORINFO_CONTINUATION_CONTINUE_ON_THREAD_POOL flag set
118130

119131
# Exception handling behavior
120132

121-
If an async function is called within a try block (In the jit hasTryIndex return true), set the CORINFO\_CONTINUATION\_HAS\_EXCEPTION bit on the Continuation and make it large enough.
133+
If an async function is called within a try block (In the jit hasTryIndex return true), allocate space for an exception in the continuation and encode its offset in the flags based on the scheme above.
122134

123135
# Locals handling
124136

src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\GenericsHelpers.cs" />
207207
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\InitHelpers.cs" />
208208
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\AsyncHelpers.CoreCLR.cs" />
209+
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\AsyncProfiler.CoreCLR.cs" />
209210
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeHelpers.CoreCLR.cs" />
210211
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\StaticsHelpers.cs" />
211212
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\VirtualDispatchHelpers.cs" />

0 commit comments

Comments
 (0)