Skip to content
Open
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 @@ -3092,6 +3092,56 @@ static void Validate(ReadyToRunReader reader)
}
}

/// <summary>
/// The Task-returning thunk of an in-module runtime-async method has no IL body at runtime, so a
/// cross-module compiled instantiation of it must use the async variant for its own IL body fixup.
/// </summary>
[ConditionalFact(typeof(TestPaths), nameof(TestPaths.IsNotWasmTarget))]
public void RuntimeAsyncThunkILBodyFixup()
{
var asm = new CompiledAssembly
{
AssemblyName = nameof(RuntimeAsyncThunkILBodyFixup),
SourceResourceNames =
[
"RuntimeAsync/AsyncThunkOverCoreLibGeneric.cs",
],
Features = { RuntimeAsyncFeature },
};

new R2RTestRunner(_output).Run(new R2RTestCase(
nameof(RuntimeAsyncThunkILBodyFixup),
[
new(nameof(RuntimeAsyncThunkILBodyFixup), [new CrossgenAssembly(asm)])
{
Options = [Crossgen2Option.Optimize],
AdditionalArgs = ["--opt-cross-module", "*"],
Validate = Validate,
},
]));

static void Validate(ReadyToRunReader reader)
{
var formattingOptions = new SignatureFormattingOptions();
List<ReadyToRunMethod> thunks = R2RAssert.GetAllMethods(reader)
.Where(m => m.Name == "PassThrough"
&& m.SignatureString.Contains("ValueTuple", StringComparison.Ordinal)
&& !m.SignatureString.Contains("[ASYNC]", StringComparison.Ordinal)
&& !m.SignatureString.Contains("[RESUME]", StringComparison.Ordinal))
.ToList();
Assert.True(thunks.Count == 1,
$"Expected one Task-returning PassThrough over ValueTuple, found: [{string.Join(", ", thunks.Select(m => m.SignatureString))}]");

List<string> ilBodyFixups = (thunks[0].Fixups ?? Array.Empty<FixupCell>())
.Where(f => f.Signature?.FixupKind == ReadyToRunFixupKind.Check_IL_Body)
.Select(f => f.Signature.ToString(formattingOptions))
.Where(s => s.Contains("AsyncThunkOverCoreLibGeneric.PassThrough", StringComparison.Ordinal))
.ToList();
Assert.True(ilBodyFixups.Count == 1 && ilBodyFixups[0].StartsWith("[ASYNC] ", StringComparison.Ordinal),
$"Expected one Check_IL_Body fixup for the async variant of PassThrough, found: [{string.Join(", ", ilBodyFixups)}]");
}
}

/// <summary>
/// Validates that ResumptionStubEntryPoint fixups are deduplicated for a method:
/// even with multiple suspension points and forced compilation retries (via
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Test: Task-returning thunk of an in-module runtime-async generic method
// instantiated over a shared System.Private.CoreLib generic struct.
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public static class AsyncThunkOverCoreLibGeneric
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static async Task<T> PassThrough<T>(T value)
{
await Task.Yield();
return value;
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static Task<ValueTuple<object, object>> CallPassThrough(object first, object second)
=> PassThrough((first, second));
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,29 @@ public ILBodyFixupSignature(ReadyToRunFixupKind fixupKind, MethodDesc signatureM
/// <summary>
/// Returns the method a compiled method's own IL body fixup is created for, or null if the compiled method cannot have one.
/// </summary>
/// <remarks>
/// A compiler-generated async thunk has no IL header at runtime, so it is checked using the other variant of
/// the same method. Both variants hash the same metadata IL and impl flags, so the check still fails if the
/// method's body or async-ness changes.
/// </remarks>
public static MethodDesc GetSignatureMethodForCompiledMethod(MethodDesc compiledMethod)
{
MethodDesc typicalDef = compiledMethod.GetTypicalMethodDefinition();
return typicalDef is EcmaMethod or AsyncMethodVariant ? typicalDef : null;
if (typicalDef is not (EcmaMethod or AsyncMethodVariant))
return null;

if (typicalDef.IsCompilerGeneratedILBodyForAsync())
{
if (typicalDef is AsyncMethodVariant)
return typicalDef.GetPrimaryMethodDesc();

// Only Task-returning runtime-async methods have a thunk and an async variant. The others only exist in
// CoreLib and are [BypassReadyToRun].
Debug.Assert(typicalDef.Signature.ReturnsTaskOrValueTask());
return typicalDef.GetAsyncVariant();
}

return typicalDef;
}

public override int ClassCode => 308579267;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
{
inlinerIdentity = ILBodyFixupSignature.GetSignatureMethodForCompiledMethod(inliner);

// Cross-module inliners are encoded by their own Check_IL_Body import. Thunks and stubs don't have one.
if (inlinerIdentity is null || inlinerIdentity.IsCompilerGeneratedILBodyForAsync())
// Cross-module inliners are encoded by their own Check_IL_Body import. Stubs don't have one.
if (inlinerIdentity is null)
continue;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ public void CreateCrossModuleInlineableTokensForILBody(MethodDesc method)
Debug.Assert(!_compilationModuleGroup.VersionsWithMethodBody(method) &&
_compilationModuleGroup.CrossModuleInlineable(method));

// The wrapped IL is the metadata IL, which for a Task-returning runtime-async method is the body of its
// async variant, not of its thunk. Thunks are never compiled outside the version bubble or inlined
// because their IL has a catch clause, so they must never get here.
Debug.Assert(!NeedsTaskReturningThunk(method));

if (!wrappedMethodIL.Initialize(_manifestMutableModule, EcmaMethodIL.Create((EcmaMethod)method.GetPrimaryMethodDesc().GetTypicalMethodDefinition())))
{
// If we could not initialize the wrapped method IL, we should store a null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,14 +869,14 @@ private ManagedHelperProbeResult CompileMethod(MethodWithGCInfo methodCodeNodeNe
}

var typicalDef = MethodBeingCompiled.GetTypicalMethodDefinition();
if (ILBodyFixupSignature.GetSignatureMethodForCompiledMethod(MethodBeingCompiled) is not null)
if (ILBodyFixupSignature.GetSignatureMethodForCompiledMethod(MethodBeingCompiled) is MethodDesc ilBodySignatureMethod)
{
var ecmaMethod = (EcmaMethod)typicalDef.GetPrimaryMethodDesc();
if ((methodIL.GetMethodILScopeDefinition() is IEcmaMethodIL && _compilation.SymbolNodeFactory.VerifyTypeAndFieldLayout && ecmaMethod.Module == typicalDef.Context.SystemModule) ||
(!_compilation.NodeFactory.CompilationModuleGroup.VersionsWithMethodBody(MethodBeingCompiled) &&
_compilation.NodeFactory.CompilationModuleGroup.CrossModuleInlineable(MethodBeingCompiled)))
{
ISymbolNode ilBodyNode = _compilation.SymbolNodeFactory.CheckILBodyFixupSignature(typicalDef);
ISymbolNode ilBodyNode = _compilation.SymbolNodeFactory.CheckILBodyFixupSignature(ilBodySignatureMethod);
AddPrecodeFixup(ilBodyNode);
}

Expand Down
Loading