From 6e40d2ba45adece19da571655d290585f94c4ba9 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Fri, 26 Jun 2026 09:50:55 +0200 Subject: [PATCH] Avoid stripping IL of non-async Task-returning methods The IL of these may be needed at runtime to compile async versions. --- .../ReadyToRun/CopiedMethodILNode.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/CopiedMethodILNode.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/CopiedMethodILNode.cs index 773a7e3b56c00c..ab0d5c17ca6952 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/CopiedMethodILNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/CopiedMethodILNode.cs @@ -5,6 +5,7 @@ using System.Reflection.Metadata; using Internal.Text; +using Internal.TypeSystem; using Internal.TypeSystem.Ecma; using Debug = System.Diagnostics.Debug; @@ -57,8 +58,7 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false) if (factory.OptimizationFlags.StripILBodies && factory.OptimizationFlags.CompiledMethodDefs.Contains(_method) - && !_method.HasInstantiation - && !_method.OwningType.HasInstantiation) + && !MayNeedILAtRuntime(_method)) { return new ObjectData(s_minimalILBody, Array.Empty(), 4, new ISymbolDefinitionNode[] { this }); } @@ -72,6 +72,23 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false) return new ObjectData(bodyBytes, Array.Empty(), 4, new ISymbolDefinitionNode[] { this }); } + private static bool MayNeedILAtRuntime(MethodDesc method) + { + if (method.HasInstantiation || method.OwningType.HasInstantiation) + { + // IL may be needed for new instantiations + return true; + } + + if (method.GetTypicalMethodDefinition().Signature.ReturnsTaskOrValueTask() && !method.IsAsync) + { + // IL may be needed for async version of non-async Task-returning method + return true; + } + + return false; + } + public override int ClassCode => 541651465; public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)