Skip to content

Commit bfc3d75

Browse files
jtschusterCopilot
authored andcommitted
Remove async method inlining restrictions in crossgen2 (dotnet#125472)
Remove restrictions that prevented async methods from being inlined during ReadyToRun compilation. All 69 async tests pass with both crossgen2 and composite R2R. Output of the src/tests/async/execution-context/execution-context.cs was validated manually to ensure inlining is happening for async calls to methods without awaits. In `Test()`, these two calls were inlined as expected. https://github.com/dotnet/runtime/blob/b7973489277b74bc5da9d36a0a2883eaa987328f/src/tests/async/execution-context/execution-context.cs#L110-L121 Fixes dotnet#124665 --------- Co-authored-by: Copilot <[email protected]>
1 parent 09c6bae commit bfc3d75

13 files changed

Lines changed: 438 additions & 91 deletions

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ILBodyFixupSignature.cs‎

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,42 @@
1313
using Internal.CorConstants;
1414
using Internal.JitInterface;
1515

16+
using ILCompiler.ReadyToRun.TypeSystem;
17+
1618
namespace ILCompiler.DependencyAnalysis.ReadyToRun
1719
{
20+
/// <summary>
21+
/// This fixup instructs the runtime to validate that the IL found at runtime matches the hash of the IL computed at compile time.
22+
/// </summary>
23+
/// <remarks>
24+
/// The fixup encodes two distinct pieces of information:
25+
/// <list type="bullet">
26+
/// <item>The IL body hash, computed from the <see cref="ILMethod"/> (the underlying EcmaMethod whose metadata
27+
/// contains the IL). This is derived from _signatureMethod via GetPrimaryMethodDesc().</item>
28+
/// <item>The method identity (_signatureMethod), encoded in the fixup signature. The runtime decodes this
29+
/// back to a MethodDesc via ZapSig::DecodeMethod and reads the IL at that method's RVA to compare against
30+
/// the hash.</item>
31+
/// </list>
32+
/// For most methods, _signatureMethod is already the EcmaMethod. For runtime-async methods, the JIT inlines
33+
/// an AsyncMethodVariant. The EcmaMethod for AsyncMethodVariant can be retrieved with GetPrimaryMethodDesc().
34+
/// </remarks>
1835
public class ILBodyFixupSignature : Signature, IEquatable<ILBodyFixupSignature>
1936
{
2037
private readonly ReadyToRunFixupKind _fixupKind;
21-
private readonly EcmaMethod _method;
2238

23-
public ILBodyFixupSignature(ReadyToRunFixupKind fixupKind, EcmaMethod ecmaMethod)
39+
/// <summary>The method identity encoded in the fixup signature that the runtime decodes to locate the IL at its RVA.</summary>
40+
private readonly MethodDesc _signatureMethod;
41+
42+
/// <summary>The underlying EcmaMethod whose IL body from metadata is hashed for the fixup validation.</summary>
43+
private EcmaMethod ILMethod => (EcmaMethod)_signatureMethod.GetPrimaryMethodDesc();
44+
45+
public ILBodyFixupSignature(ReadyToRunFixupKind fixupKind, MethodDesc signatureMethod)
2446
{
47+
Debug.Assert(signatureMethod.IsTypicalMethodDefinition);
48+
Debug.Assert(!signatureMethod.IsCompilerGeneratedILBodyForAsync());
49+
Debug.Assert(signatureMethod.GetPrimaryMethodDesc() is EcmaMethod);
2550
_fixupKind = fixupKind;
26-
_method = ecmaMethod;
51+
_signatureMethod = signatureMethod;
2752
}
2853

2954
public override int ClassCode => 308579267;
@@ -44,10 +69,11 @@ public static void NotifyComplete(NodeFactory factory, List<ILBodyFixupSignature
4469

4570
private ModuleToken GetModuleToken(NodeFactory factory)
4671
{
47-
if (factory.CompilationModuleGroup.VersionsWithMethodBody(_method))
48-
return new ModuleToken(_method.Module, _method.Handle);
72+
EcmaMethod ilMethod = ILMethod;
73+
if (factory.CompilationModuleGroup.VersionsWithMethodBody(ilMethod))
74+
return new ModuleToken(ilMethod.Module, ilMethod.Handle);
4975
else
50-
return new ModuleToken(factory.ManifestMetadataTable._mutableModule, factory.ManifestMetadataTable._mutableModule.TryGetEntityHandle(_method.GetTypicalMethodDefinition()).Value);
76+
return new ModuleToken(factory.ManifestMetadataTable._mutableModule, factory.ManifestMetadataTable._mutableModule.TryGetEntityHandle(ilMethod.GetTypicalMethodDefinition()).Value);
5177
}
5278

5379
public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
@@ -63,7 +89,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
6389
IEcmaModule targetModule = moduleToken.Module;
6490
SignatureContext innerContext = dataBuilder.EmitFixup(factory, _fixupKind, targetModule, factory.SignatureContext);
6591

66-
var metadata = ReadyToRunStandaloneMethodMetadata.Compute(_method);
92+
var metadata = ReadyToRunStandaloneMethodMetadata.Compute(ILMethod);
6793
dataBuilder.EmitUInt(checked((uint)metadata.ConstantData.Length));
6894
dataBuilder.EmitBytes(metadata.ConstantData);
6995
dataBuilder.EmitUInt(checked((uint)metadata.TypeRefs.Length));
@@ -77,7 +103,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
77103
dataBuilder.EmitTypeSignature(typeRef, innerContext);
78104
}
79105

80-
MethodWithToken method = new MethodWithToken(_method, moduleToken, null, unboxing: false, context: null);
106+
MethodWithToken method = new MethodWithToken(_signatureMethod, moduleToken, null, unboxing: false, context: null);
81107
dataBuilder.EmitMethodSignature(method, enforceDefEncoding: false, enforceOwningType: false, innerContext, false);
82108
}
83109

@@ -88,7 +114,7 @@ public override void AppendMangledName(NameMangler nameMangler, Utf8StringBuilde
88114
{
89115
sb.Append(nameMangler.CompilationUnitPrefix);
90116
sb.Append($@"ILBodyFixupSignature({_fixupKind.ToString()}): ");
91-
sb.Append(nameMangler.GetMangledMethodName(_method));
117+
sb.Append(nameMangler.GetMangledMethodName(ILMethod));
92118
}
93119

94120
public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
@@ -98,12 +124,12 @@ public override int CompareToImpl(ISortableNode other, CompilerComparer comparer
98124
if (result != 0)
99125
return result;
100126

101-
return comparer.Compare(_method, otherNode._method);
127+
return comparer.Compare(_signatureMethod, otherNode._signatureMethod);
102128
}
103129

104130
public override string ToString()
105131
{
106-
return $"ILBodyFixupSignature {_fixupKind} {_method}";
132+
return $"ILBodyFixupSignature {_fixupKind} {_signatureMethod}";
107133
}
108134

109135
public bool Equals(ILBodyFixupSignature other) => object.ReferenceEquals(other, this);

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/InliningInfoNode.cs‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
6060
if (relocsOnly)
6161
return new ObjectData(Array.Empty<byte>(), Array.Empty<Relocation>(), 1, new ISymbolDefinitionNode[] { this });
6262

63-
Dictionary<EcmaMethod, HashSet<EcmaMethod>> inlineeToInliners = new Dictionary<EcmaMethod, HashSet<EcmaMethod>>();
63+
Dictionary<MethodDesc, HashSet<MethodDesc>> inlineeToInliners = new Dictionary<MethodDesc, HashSet<MethodDesc>>();
6464

6565
// Build a map from inlinee to the list of inliners
6666
// We are only interested in the generic definitions of these.
@@ -72,11 +72,6 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
7272
continue;
7373
}
7474
MethodDesc inliner = methodNode.Method;
75-
if (inliner.IsCompilerGeneratedILBodyForAsync())
76-
{
77-
// Async thunks are restricted from inlining. https://github.com/dotnet/runtime/issues/124665
78-
continue;
79-
}
8075
EcmaMethod inlinerDefinition = (EcmaMethod)inliner.GetPrimaryMethodDesc().GetTypicalMethodDefinition();
8176

8277
if (inlinerDefinition.IsNonVersionable())
@@ -93,7 +88,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
9388
foreach (MethodDesc inlinee in inlinees)
9489
{
9590
MethodDesc inlineeDefinition = inlinee.GetTypicalMethodDefinition();
96-
if (!(inlineeDefinition is EcmaMethod ecmaInlineeDefinition))
91+
if (!(inlineeDefinition is EcmaMethod or AsyncMethodVariant))
9792
{
9893
// We don't record non-ECMA methods because they don't have tokens that
9994
// diagnostic tools could reason about anyway.
@@ -114,7 +109,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
114109
{
115110
if (!inlinerReportAllVersionsWithInlinee)
116111
{
117-
// We'll won't report this method
112+
// We won't report this method
118113
continue;
119114
}
120115
}
@@ -123,17 +118,17 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
123118
Debug.Assert(factory.CompilationModuleGroup.CrossModuleInlineable(inlineeDefinition));
124119
if (_inlineInfoType != InfoType.CrossModuleInliningForCrossModuleDataOnly)
125120
{
126-
// We'll won't report this method
121+
// We won't report this method
127122
continue;
128123
}
129124
}
130125

131-
if (!inlineeToInliners.TryGetValue(ecmaInlineeDefinition, out HashSet<EcmaMethod> inliners))
126+
if (!inlineeToInliners.TryGetValue(inlineeDefinition, out HashSet<MethodDesc> inliners))
132127
{
133-
inliners = new HashSet<EcmaMethod>();
134-
inlineeToInliners.Add(ecmaInlineeDefinition, inliners);
128+
inliners = new HashSet<MethodDesc>();
129+
inlineeToInliners.Add(inlineeDefinition, inliners);
135130
}
136-
inliners.Add((EcmaMethod)inlinerDefinition);
131+
inliners.Add(inlinerDefinition);
137132
}
138133
}
139134

@@ -146,8 +141,9 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
146141

147142
foreach (var inlineeWithInliners in inlineeToInliners)
148143
{
149-
EcmaMethod inlinee = inlineeWithInliners.Key;
150-
int inlineeRid = MetadataTokens.GetRowNumber(inlinee.Handle);
144+
MethodDesc inlinee = inlineeWithInliners.Key;
145+
EcmaMethod ecmaInlinee = (EcmaMethod)inlinee.GetPrimaryMethodDesc();
146+
int inlineeRid = MetadataTokens.GetRowNumber(ecmaInlinee.Handle);
151147
int hashCode;
152148

153149
if (AllowCrossModuleInlines)
@@ -158,7 +154,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
158154
else
159155
{
160156
// InliningInfo2 format
161-
hashCode = VersionResilientHashCode.ModuleNameHashCode(inlinee.Module);
157+
hashCode = VersionResilientHashCode.ModuleNameHashCode(ecmaInlinee.Module);
162158
hashCode ^= inlineeRid;
163159
}
164160

@@ -173,14 +169,19 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
173169
// Followed by inliner RIDs deltas with flag in the lowest bit
174170
// - if flag is set, followed by module ID
175171
Debug.Assert(_module != null);
176-
bool isForeignInlinee = inlinee.Module != _module;
172+
bool isForeignInlinee = ecmaInlinee.Module != _module;
177173
sig.Append(new UnsignedConstant((uint)(inlineeRid << 1 | (isForeignInlinee ? 1 : 0))));
178174
if (isForeignInlinee)
179175
{
180-
sig.Append(new UnsignedConstant((uint)factory.ManifestMetadataTable.ModuleToIndex(inlinee.Module)));
176+
sig.Append(new UnsignedConstant((uint)factory.ManifestMetadataTable.ModuleToIndex(ecmaInlinee.Module)));
181177
}
182178

183-
List<EcmaMethod> sortedInliners = new List<EcmaMethod>(inlineeWithInliners.Value);
179+
// We're only concerned with metadata here, so we can convert all to EcmaMethod and lose info about AsyncVariant vs Task-Returning
180+
List<EcmaMethod> sortedInliners = new List<EcmaMethod>(inlineeWithInliners.Value.Count);
181+
foreach (var inliner in inlineeWithInliners.Value)
182+
{
183+
sortedInliners.Add((EcmaMethod)inliner.GetPrimaryMethodDesc());
184+
}
184185
sortedInliners.MergeSort((a, b) =>
185186
{
186187
if (a == b)
@@ -302,7 +303,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
302303
}
303304
else
304305
{
305-
indexOfInlinee = (uint)MetadataTokens.GetRowNumber(inlinee.Handle);
306+
indexOfInlinee = (uint)MetadataTokens.GetRowNumber(ecmaInlinee.Handle);
306307
}
307308

308309
encodedInlinee = indexOfInlinee << (int)ReadyToRunCrossModuleInlineFlags.CrossModuleInlinerIndexShift;
@@ -315,7 +316,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
315316

316317
sig.Append(new UnsignedConstant(encodedInlinee));
317318
if (crossModuleMultiModuleFormat && !isCrossModuleInlinee)
318-
sig.Append(new UnsignedConstant((uint)factory.ManifestMetadataTable.ModuleToIndex(inlinee.Module)));
319+
sig.Append(new UnsignedConstant((uint)factory.ManifestMetadataTable.ModuleToIndex(ecmaInlinee.Module)));
319320

320321
int inlinerIndex = 0;
321322
if (crossModuleInlinerCount > 0)
@@ -339,7 +340,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
339340
uint inlinerRid = (uint)MetadataTokens.GetRowNumber(inliner.Handle);
340341
uint ridDelta = inlinerRid - baseRid;
341342
baseRid = inlinerRid;
342-
bool isForeignInliner = inliner.Module != inlinee.Module;
343+
bool isForeignInliner = inliner.Module != ecmaInlinee.Module;
343344
Debug.Assert(!isForeignInliner || crossModuleMultiModuleFormat);
344345

345346
if (crossModuleMultiModuleFormat)

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunCodegenNodeFactory.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,12 @@ public VirtualResolutionFixupSignature VirtualResolutionFixupSignature(ReadyToRu
644644
private struct ILBodyFixupSignatureFixupKey : IEquatable<ILBodyFixupSignatureFixupKey>
645645
{
646646
public readonly ReadyToRunFixupKind FixupKind;
647-
public readonly EcmaMethod Method;
647+
public readonly MethodDesc Method;
648648

649-
public ILBodyFixupSignatureFixupKey(ReadyToRunFixupKind fixupKind, EcmaMethod method)
649+
public ILBodyFixupSignatureFixupKey(ReadyToRunFixupKind fixupKind, MethodDesc method)
650650
{
651651
FixupKind = fixupKind;
652+
Debug.Assert(method.IsTypicalMethodDefinition);
652653
Method = method;
653654
}
654655
public bool Equals(ILBodyFixupSignatureFixupKey other) => FixupKind == other.FixupKind && Method.Equals(other.Method);
@@ -660,7 +661,7 @@ public ILBodyFixupSignatureFixupKey(ReadyToRunFixupKind fixupKind, EcmaMethod me
660661
private NodeCache<ILBodyFixupSignatureFixupKey, ILBodyFixupSignature> _ilBodySignatures =
661662
new NodeCache<ILBodyFixupSignatureFixupKey, ILBodyFixupSignature>((key) => new ILBodyFixupSignature(key.FixupKind, key.Method));
662663

663-
public ILBodyFixupSignature ILBodyFixupSignature(ReadyToRunFixupKind fixupKind, EcmaMethod method)
664+
public ILBodyFixupSignature ILBodyFixupSignature(ReadyToRunFixupKind fixupKind, MethodDesc method)
664665
{
665666
return _ilBodySignatures.GetOrAdd(new ILBodyFixupSignatureFixupKey(fixupKind, method));
666667
}

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRunSymbolNodeFactory.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Internal.TypeSystem.Ecma;
1010
using Internal.ReadyToRunConstants;
1111
using Internal.Text;
12+
using System.Diagnostics;
1213

1314
namespace ILCompiler.DependencyAnalysis
1415
{
@@ -507,8 +508,9 @@ public Import CheckVirtualFunctionOverride(MethodWithToken declMethod, TypeDesc
507508
}
508509

509510
private NodeCache<ILBodyFixupSignature, Import> _ilBodyFixupsCache;
510-
public Import CheckILBodyFixupSignature(EcmaMethod method)
511+
public Import CheckILBodyFixupSignature(MethodDesc method)
511512
{
513+
Debug.Assert(method.IsTypicalMethodDefinition);
512514
return _ilBodyFixupsCache.GetOrAdd(_codegenNodeFactory.ILBodyFixupSignature(
513515
_verifyTypeAndFieldLayout ? ReadyToRunFixupKind.Verify_IL_Body : ReadyToRunFixupKind.Check_IL_Body,
514516
method));

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,6 @@ public bool CanInline(MethodDesc caller, MethodDesc callee)
109109
}
110110
}
111111

112-
// TODO: Enable async inlining. https://github.com/dotnet/runtime/issues/124665
113-
if (callee.IsAsyncThunk() || callee.IsAsyncCall())
114-
{
115-
return false;
116-
}
117-
118112
_nodeFactory.DetectGenericCycles(caller, callee);
119113

120114
return NodeFactory.CompilationModuleGroup.CanInline(caller, callee);
@@ -655,14 +649,19 @@ public bool IsInheritanceChainLayoutFixedInCurrentVersionBubble(TypeDesc type)
655649
// The _finishedFirstCompilationRunInPhase2 variable works in concert some checking to ensure that we don't violate any of this model
656650
private bool _finishedFirstCompilationRunInPhase2 = false;
657651

658-
public void PrepareForCompilationRetry(MethodWithGCInfo methodToBeRecompiled, IEnumerable<EcmaMethod> methodsThatNeedILBodies)
652+
public void PrepareForCompilationRetry(MethodWithGCInfo methodToBeRecompiled, IEnumerable<MethodDesc> methodsThatNeedILBodies)
659653
{
660654
lock (_methodsToRecompile)
661655
{
662656
_methodsToRecompile.Add(methodToBeRecompiled);
663657
if (methodsThatNeedILBodies != null)
658+
{
664659
foreach (var method in methodsThatNeedILBodies)
660+
{
661+
Debug.Assert(method.IsMethodDefinition);
665662
_methodsWhichNeedMutableILBodies.Add(method);
663+
}
664+
}
666665
}
667666
}
668667

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCompilationModuleGroupBase.cs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,6 @@ private bool CrossModuleInlineableInternal(MethodDesc method)
520520

521521
private bool CrossModuleInlineableUncached(MethodDesc method)
522522
{
523-
// Async thunks and variants cannot currently be inlined cross module
524-
if (method.IsAsyncVariant() || method.IsAsync || method.IsAsyncThunk())
525-
return false;
526-
527523
// Defined in corelib
528524
MetadataType owningMetadataType = method.OwningType.GetTypeDefinition() as MetadataType;
529525
if (owningMetadataType == null)

0 commit comments

Comments
 (0)