Skip to content
Merged
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
14 changes: 1 addition & 13 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5864,12 +5864,6 @@ GenTree* Lowering::LowerAsyncContinuation(GenTree* asyncCont)
// 2. In the async transformation, ASYNC_CONTINUATION nodes are inserted
// after calls to async calls.
//
// In the former case, for NativeAOT nothing has marked the previous call
// as an "async" method. We need to do that here to ensure that the backend
// knows that the call has a non-standard calling convention that returns
// an additional GC ref. This requires additional GC tracking that we would
// otherwise not get.
//
GenTree* node = asyncCont;
while (true)
{
Expand All @@ -5878,13 +5872,7 @@ GenTree* Lowering::LowerAsyncContinuation(GenTree* asyncCont)

if (node->IsCall())
{
if (!node->AsCall()->IsAsync())
{
JITDUMP("Marking the call [%06u] before async continuation [%06u] as an async call\n",
Compiler::dspTreeID(node), Compiler::dspTreeID(asyncCont));
node->AsCall()->SetIsAsync(new (comp, CMK_Async) AsyncCallInfo);
}

assert(node->AsCall()->IsAsync());
BlockRange().Remove(asyncCont);
BlockRange().InsertAfter(node, asyncCont);
break;
Expand Down
5 changes: 0 additions & 5 deletions src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -842,11 +842,6 @@ private void Get_CORINFO_SIG_INFO(MethodDesc method, CORINFO_SIG_INFO* sig, Meth
if (method.IsIntrinsic)
{
// Some intrinsics will beg to differ about the hasHiddenParameter decision
#if !READYTORUN
if (_compilation.TypeSystemContext.IsSpecialUnboxingThunkTargetMethod(method))
hasHiddenParameter = false;
#endif

if (method.IsArrayAddressMethod())
hasHiddenParameter = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,4 @@ protected override int CompareToImpl(MethodDesc other, TypeSystemComparer compar
return comparer.Compare(_targetMethod, ((AsyncResumptionStub)other)._targetMethod);
}
}

internal sealed partial class ExplicitContinuationAsyncMethod : MethodDesc
{
protected override int ClassCode => 0xd076659;

protected override int CompareToImpl(MethodDesc other, TypeSystemComparer comparer)
{
return comparer.Compare(_wrappedMethod, ((ExplicitContinuationAsyncMethod)other)._wrappedMethod);
}
}
}
164 changes: 11 additions & 153 deletions src/coreclr/tools/Common/TypeSystem/IL/Stubs/AsyncResumptionStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ public override MethodIL EmitIL()
}
}

if (Context.Target.Architecture != TargetArchitecture.X86)
if (_targetMethod.RequiresInstArg())
{
if (_targetMethod.RequiresInstArg())
{
ilStream.EmitLdc(0);
ilStream.Emit(ILOpcode.conv_i);
}
ilStream.EmitLdArg(0);
ilStream.EmitLdc(0);
ilStream.Emit(ILOpcode.conv_i);
ilStream.Emit(ILOpcode.call, ilEmitter.NewToken(Context.GetCoreLibEntryPoint("System.Runtime.CompilerServices"u8, "RuntimeHelpers"u8, "SetNextCallGenericContext"u8, null)));
}

ilStream.EmitLdArg(0);
ilStream.Emit(ILOpcode.call, ilEmitter.NewToken(Context.GetCoreLibEntryPoint("System.Runtime.CompilerServices"u8, "RuntimeHelpers"u8, "SetNextCallAsyncContinuation"u8, null)));

foreach (var param in _targetMethod.Signature)
{
var local = ilEmitter.NewLocal(param);
Expand All @@ -78,24 +78,13 @@ public override MethodIL EmitIL()
ilStream.EmitLdLoc(local);
}

if (Context.Target.Architecture == TargetArchitecture.X86)
{
ilStream.EmitLdArg(0);
if (_targetMethod.RequiresInstArg())
{
ilStream.EmitLdc(0);
ilStream.Emit(ILOpcode.conv_i);
}
}
ilStream.Emit(ILOpcode.call, ilEmitter.NewToken(_targetMethod));

MethodDesc resumingMethod = new ExplicitContinuationAsyncMethod(_targetMethod);
ilStream.Emit(ILOpcode.call, ilEmitter.NewToken(resumingMethod));

bool returnsVoid = resumingMethod.Signature.ReturnType.IsVoid;
bool returnsVoid = _targetMethod.Signature.ReturnType.IsVoid;
ILLocalVariable resultLocal = default;
if (!returnsVoid)
{
resultLocal = ilEmitter.NewLocal(resumingMethod.Signature.ReturnType);
resultLocal = ilEmitter.NewLocal(_targetMethod.Signature.ReturnType);
ilStream.EmitStLoc(resultLocal);
}

Expand All @@ -113,7 +102,7 @@ public override MethodIL EmitIL()
ilStream.Emit(ILOpcode.brtrue, doneResult);
ilStream.EmitLdArg(1);
ilStream.EmitLdLoc(resultLocal);
ilStream.Emit(ILOpcode.stobj, ilEmitter.NewToken(resumingMethod.Signature.ReturnType));
ilStream.Emit(ILOpcode.stobj, ilEmitter.NewToken(_targetMethod.Signature.ReturnType));
ilStream.EmitLabel(doneResult);
}
ilStream.EmitLdLoc(newContinuationLocal);
Expand All @@ -122,135 +111,4 @@ public override MethodIL EmitIL()
return ilEmitter.Link(this);
}
}

/// <summary>
/// A dummy method used to tell the jit that we want to explicitly pass the hidden Continuation parameter
/// as well as the generic context parameter (if any) for async resumption methods.
/// This method should be marked IsAsync=false and HasInstantiation=false. These are defaults
/// for MethodDesc and so aren't explicitly set in the code below.
/// </summary>
internal sealed partial class ExplicitContinuationAsyncMethod : MethodDesc
{
private MethodSignature _signature;
private readonly MethodDesc _wrappedMethod;
private readonly MethodDesc _typicalDefinition;

public ExplicitContinuationAsyncMethod(MethodDesc target)
{
_wrappedMethod = target;

MethodDesc targetTypicalDefinition = target.GetTypicalMethodDefinition();
if (targetTypicalDefinition != target)
_typicalDefinition = new ExplicitContinuationAsyncMethod(targetTypicalDefinition);
else
_typicalDefinition = this;
}

public MethodDesc Target => _wrappedMethod;

/// <summary>
/// To explicitly pass the hidden parameters for async resumption methods,
/// we need to add explicit Continuation and generic context parameters to the signature.
/// </summary>
private MethodSignature InitializeSignature()
{
// Async methods have an implicit Continuation parameter
// The order of parameters depends on the architecture
// non-x86: this?, genericCtx?, continuation, params...
// x86: this?, params, continuation, genericCtx?
// To make the jit pass arguments in this order, we can add the continuation parameter
// at the end for x86 and at the beginning for other architectures.
// The 'this' parameter and generic context parameter (if any) can be handled by the jit.

var parameters = new TypeDesc[_wrappedMethod.Signature.Length + 1 + (_wrappedMethod.RequiresInstArg() ? 1 : 0)];

TypeDesc continuation = Context.SystemModule.GetKnownType("System.Runtime.CompilerServices"u8, "Continuation"u8);
if (Context.Target.Architecture == TargetArchitecture.X86)
{
int i = 0;
for (; i < _wrappedMethod.Signature.Length; i++)
{
parameters[i] = _wrappedMethod.Signature[i];
}
parameters[i++] = continuation;
if (_wrappedMethod.RequiresInstArg())
{
parameters[i] = Context.GetWellKnownType(WellKnownType.Void).MakePointerType();
}
}
else
{
int i = 0;
if (_wrappedMethod.RequiresInstArg())
{
parameters[i++] = Context.GetWellKnownType(WellKnownType.Void).MakePointerType();
}
parameters[i++] = continuation;
foreach (var param in _wrappedMethod.Signature)
{
parameters[i++] = param;
}
}
// Get the return type from the Task-returning variant
TypeDesc returnType;
if (_wrappedMethod is AsyncMethodVariant variant)
{
Debug.Assert(variant.Target.Signature.ReturnsTaskOrValueTask());
if (variant.Target.Signature.ReturnType is { HasInstantiation: true } wrappedReturnType)
{
returnType = wrappedReturnType.Instantiation[0];
}
else
{
returnType = Context.GetWellKnownType(WellKnownType.Void);
}
}
else
{
// AsyncExplicitImpl
returnType = _wrappedMethod.Signature.ReturnType;
}

return _signature = new MethodSignature(
_wrappedMethod.Signature.Flags,
0,
returnType,
parameters);
}

public override bool HasCustomAttribute(string attributeNamespace, string attributeName) => throw new NotImplementedException();

public override MethodSignature Signature
{
get
{
if (_signature is null)
return InitializeSignature();

return _signature;
}
}

public override string DiagnosticName => $"ExplicitContinuationAsyncMethod({_wrappedMethod.DiagnosticName})";

public override TypeDesc OwningType => _wrappedMethod.OwningType;

public override TypeSystemContext Context => _wrappedMethod.Context;

public override bool IsInternalCall => true;

public override MethodDesc GetTypicalMethodDefinition() => _typicalDefinition;
}

public static class AsyncResumptionStubExtensions
{
public static bool IsExplicitContinuationAsyncMethod(this MethodDesc method)
{
return method is ExplicitContinuationAsyncMethod;
}
public static MethodDesc GetExplicitContinuationAsyncMethodTarget(this MethodDesc method)
{
return ((ExplicitContinuationAsyncMethod)method).Target;
}
}
}
Loading