Skip to content

Conversation

@MichalStrehovsky
Copy link
Member

When we enter ILLinker part of the ILC codebase, we need to switch to the mode where async variants don't exist and async methods have mismatched bodies. There was a spot where we didn't do the switch here:

MethodIL methodILDefinition = methodIL.GetMethodILDefinition();
if (FlowAnnotations.CompilerGeneratedState.TryGetUserMethodForCompilerGeneratedMember(methodILDefinition.OwningMethod, out var userMethod))
{
Debug.Assert(userMethod != methodILDefinition.OwningMethod);
// It is possible that this will try to add the DatadlowAnalyzedMethod node multiple times for the same method
// but that's OK since the node factory will only add actually one node.
methodILDefinition = FlowAnnotations.ILProvider.GetMethodIL(userMethod);
if (ShouldSkipDataflowForMethod(methodILDefinition))
return;
}

The fix is to do the switch early in FlowAnnotations so that the ILProvider FlowAnnotations use (and other parts take advantage of) is already ILLinkerified.

Fixes the situation shown in the regression test (we were analyzing the IL of the async variant of RuntimeAsyncWithLambda and missed analyzing the lambda completely).

Cc @dotnet/ilc-contrib

When we enter ILLinker part of the ILC codebase, we need to switch to the mode where async variants don't exist and async methods have mismatched bodies. There was a spot where we didn't do the switch here:

https://github.com/dotnet/runtime/blob/d31e5990b896447bfc3dbe98cfe6ec3b169a4896/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UsageBasedMetadataManager.cs#L939-L949

The fix is to do the switch early in `FlowAnnotations` so that the `ILProvider` `FlowAnnotations` use (and other parts take advantage of) is already ILLinkerified.

Fixes the situation shown in the regression test (we were analyzing the IL of the async variant of `RuntimeAsyncWithLambda` and missed analyzing the lambda completely).
Copilot AI review requested due to automatic review settings December 11, 2025 13:20
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Dec 11, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a dataflow analysis bug where lambdas inside async methods were not being properly analyzed. The issue occurred because the async IL masking transformation wasn't applied consistently across all dataflow analysis components.

  • Centralized AsyncMaskingILProvider wrapping in FlowAnnotations constructor
  • Removed duplicate wrapping from TypeAnnotationsHashtable and MethodBodyScanner
  • Added regression test for lambda analysis in async methods

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/FlowAnnotations.cs Wraps the ILProvider with AsyncMaskingILProvider at construction time, ensuring all consumers use the masked version; removes duplicate wrapping from TypeAnnotationsHashtable
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/MethodBodyScanner.cs Removes duplicate AsyncMaskingILProvider wrapping since FlowAnnotations now provides a pre-wrapped ILProvider
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/RuntimeAsyncMethods.cs Adds ExpectedNoWarnings attribute and new test case RuntimeAsyncWithLambda to verify lambdas in async methods are properly analyzed for dataflow warnings

@MichalStrehovsky MichalStrehovsky merged commit 1a9df91 into dotnet:main Dec 13, 2025
128 of 133 checks passed
@MichalStrehovsky MichalStrehovsky deleted the dataflowfixes branch December 13, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr linkable-framework Issues associated with delivering a linker friendly framework runtime-async

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants