Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -23,6 +23,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 Down Expand Up @@ -832,7 +837,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
3 changes: 3 additions & 0 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,9 @@ 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.
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 @@ -5006,6 +5006,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 @@ -5495,6 +5496,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 @@ -4533,6 +4533,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