Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ internal enum ContinuationFlags
ContinueOnThreadPool = 1 << 0,
ContinueOnCapturedSynchronizationContext = 1 << 1,
ContinueOnCapturedTaskScheduler = 1 << 2,
// This is an await of valueTask.AsTask() (e.g. valueTask.AsTask()
// returned from an async version). This flag affects how
// ValueTaskSourceContinuation handling computes the flags to pass to
// IValueTaskSource.OnCompleted.
ValueTaskAdaptedToTask = 1 << 3,

AllContinuationFlags = ContinueOnThreadPool | ContinueOnCapturedSynchronizationContext | ContinueOnCapturedTaskScheduler,

Comment thread
jakobbotsch marked this conversation as resolved.
Expand All @@ -30,20 +35,20 @@ internal enum ContinuationFlags
// Otherwise the exact offset of the member is computed as
// DataOffset + (index - 1) * PointerSize
//
ExecutionContextIndexFirstBit = 3,
ExecutionContextIndexFirstBit = 4,
ExecutionContextIndexNumBits = 2,

ContinuationContextIndexFirstBit = 5,
ContinuationContextIndexFirstBit = 6,
ContinuationContextIndexNumBits = 2,

ExceptionIndexFirstBit = 7,
ExceptionIndexFirstBit = 8,
ExceptionIndexNumBits = 3,

// For JIT, the continuation stores space for every possible type of
// async callee's result. We need to represent the offset to each of
// these, so we allocate the rest of the bits for this.
ResultIndexFirstBit = 10,
ResultIndexNumBits = 22,
ResultIndexFirstBit = 11,
ResultIndexNumBits = 21,
Comment thread
jakobbotsch marked this conversation as resolved.
}

// Keep in sync with CORINFO_AsyncResumeInfo in corinfo.h
Expand Down Expand Up @@ -825,7 +830,8 @@ internal unsafe bool HandleSuspended(ref RuntimeAsyncAwaitState state)
// the direct AsyncHelpers.Await(ValueTask/ValueTask<T>) path.
// In either case, that can only happen in nontransparent/user code.
Continuation contWithContinueFlags = valueTaskSourceCont;
while ((contWithContinueFlags.Flags & ContinuationFlags.AllContinuationFlags) == 0 && contWithContinueFlags.Next != null)
while ((contWithContinueFlags.Flags & (ContinuationFlags.AllContinuationFlags | ContinuationFlags.ValueTaskAdaptedToTask)) == 0 &&
contWithContinueFlags.Next != null)
{
contWithContinueFlags = contWithContinueFlags.Next;
}
Expand Down
13 changes: 8 additions & 5 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1786,26 +1786,29 @@ enum CorInfoContinuationFlags
// If this bit is set the continuation context is a TaskScheduler that
// we should continue on.
CORINFO_CONTINUATION_CONTINUE_ON_CAPTURED_TASK_SCHEDULER = 1 << 2,
// If this bit is set this is an await of valueTask.AsTask()
// (common pattern when returning a value-task in an async version)
CORINFO_CONTINUATION_VALUETASK_ADAPTED_TO_TASK = 1 << 3,

// The flags encode where in the continuation various members are stored.
// If the encoded index is 0, it means no such member is present.
// Otherwise the exact offset of the member is computed as
// OFFSETOF__CORINFO_Continuation__data + (index - 1) * PointerSize

CORINFO_CONTINUATION_EXECUTION_CONTEXT_INDEX_FIRST_BIT = 3,
CORINFO_CONTINUATION_EXECUTION_CONTEXT_INDEX_FIRST_BIT = 4,
CORINFO_CONTINUATION_EXECUTION_CONTEXT_INDEX_NUM_BITS = 2,

CORINFO_CONTINUATION_CONTEXT_INDEX_FIRST_BIT = 5,
CORINFO_CONTINUATION_CONTEXT_INDEX_FIRST_BIT = 6,
CORINFO_CONTINUATION_CONTEXT_INDEX_NUM_BITS = 2,

CORINFO_CONTINUATION_EXCEPTION_INDEX_FIRST_BIT = 7,
CORINFO_CONTINUATION_EXCEPTION_INDEX_FIRST_BIT = 8,
CORINFO_CONTINUATION_EXCEPTION_INDEX_NUM_BITS = 3,

// For JIT, the continuation stores space for every possible type of
// async callee's result. We need to represent the offset to each of
// these, so we allocate the rest of the bits for this.
CORINFO_CONTINUATION_RESULT_INDEX_FIRST_BIT = 10,
CORINFO_CONTINUATION_RESULT_INDEX_NUM_BITS = 22,
CORINFO_CONTINUATION_RESULT_INDEX_FIRST_BIT = 11,
CORINFO_CONTINUATION_RESULT_INDEX_NUM_BITS = 21,
};

struct CORINFO_ASYNC_INFO
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,11 @@ void AsyncTransformation::CreateSuspension(BasicBlock* call
continuationFlags |= CORINFO_CONTINUATION_CONTINUE_ON_THREAD_POOL;
}

if (callInfo.IsValueTaskAsTask)
{
continuationFlags |= CORINFO_CONTINUATION_VALUETASK_ADAPTED_TO_TASK;
}

newContinuation = m_compiler->gtNewLclvNode(newContinuationVar, TYP_REF);
unsigned flagsOffset = m_compiler->info.compCompHnd->getFieldOffset(m_asyncInfo->continuationFlagsFldHnd);
GenTree* flagsNode = m_compiler->gtNewIconNode((ssize_t)continuationFlags, TYP_INT);
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5001,6 +5001,7 @@ class Compiler
PREFIX_IS_TASK_AWAIT = 0x00000080,
PREFIX_TASK_AWAIT_CONTINUE_ON_CAPTURED_CONTEXT = 0x00000100,
PREFIX_IS_ASYNC_VERSION_TAIL_AWAIT = 0x00000200,
PREFIX_IS_ADAPTED_FROM_VALUETASK = 0x00000400,
};

static void impValidateMemoryAccessOpcode(const BYTE* codeAddr, const BYTE* codeEndp, bool volatilePrefix);
Expand Down Expand Up @@ -5480,6 +5481,9 @@ class Compiler
bool impMatchIsInstBooleanConversion(const BYTE* codeAddr, const BYTE* codeEndp, int* consumed);

const BYTE* impMatchTaskAwaitPattern(const BYTE* codeAddr, const BYTE* codeEndp, int* configVal, IL_OFFSET* awaitOffset);
bool impMatchAsyncVersionTailCall(const BYTE* codeAddr, const BYTE* codeEndp, int* prefixFlags, int* numBytesMatched);
bool impMatchStlocLdloca(const BYTE** codeAddr, const BYTE* codeEndp, unsigned* lclNum);

bool impCheckOptimizeAwait(IL_OFFSET awaitOffset);

GenTree* impCastClassOrIsInstToTree(
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4535,6 +4535,11 @@ struct AsyncCallInfo
// records that behavior.
::ContinuationContextHandling ContinuationContextHandling = ContinuationContextHandling::None;

// Is this 'await valueTask.AsTask()'? These come with special semantics as
// they no longer transparently forward continuation context handling to an
// underlying IValueTaskSource, if present.
bool IsValueTaskAsTask = false;

// Tail awaits do not generate suspension points and the JIT instead
// directly returns the callee's continuation to the caller.
bool IsTailAwait = false;
Expand Down
Loading
Loading