Skip to content

Centralize virtual method invocation dependencies - #121740

Merged
MichalStrehovsky merged 1 commit into
dotnet:mainfrom
MichalStrehovsky:reflectmethod
Nov 18, 2025
Merged

MichalStrehovsky merged 1 commit into
dotnet:mainfrom
MichalStrehovsky:reflectmethod

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

ReflectionVirtualInvokeMapNode should be solely responsible for declaring dependencies necessary to generate the invocation table entries. Instead we had them partially duplicated between ReflectedMethodNode and ReflectionVirtualInvokeMapNode.GetVirtualInvokeMapDependencies.

This can lead to subtle problems in the unsupported IlcTrimMetadata==false mode that needs this work to happen in ReflectionVirtualInvokeMapNode since we don't make ReflectedMethodNode there (because everything is reflected).

Cc @dotnet/ilc-contrib

`ReflectionVirtualInvokeMapNode` should be solely responsible for declaring dependencies necessary to generate the invocation table entries. Instead we had them partially duplicated between `ReflectedMethodNode` and `ReflectionVirtualInvokeMapNode.GetVirtualInvokeMapDependencies`.

This can lead to subtle problems in the unsupported `IlcTrimMetadata`==`false` mode that needs this work to happen in `ReflectionVirtualInvokeMapNode` since we don't make `ReflectedMethodNode` there (because everything is reflected).
Copilot AI review requested due to automatic review settings November 18, 2025 11:49
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 refactors the dependency analysis for virtual method invocation in reflection scenarios by centralizing the logic in ReflectionVirtualInvokeMapNode. Previously, these dependencies were duplicated between ReflectedMethodNode and ReflectionVirtualInvokeMapNode.GetVirtualInvokeMapDependencies.

Key changes:

  • Moved virtual method invocation dependency logic entirely from ReflectedMethodNode to ReflectionVirtualInvokeMapNode.GetVirtualInvokeMapDependencies
  • Removed early return for generic definitions in ReflectedMethodNode, as these checks are now handled through the metadata category and reflection invokability checks
  • Consolidated the handling of both instantiated and non-instantiated virtual methods in GetVirtualInvokeMapDependencies

Reviewed Changes

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

File Description
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectedMethodNode.cs Removes virtual method invocation dependencies logic and generic definition early return, simplifying the node to focus on reflection visibility concerns
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectionVirtualInvokeMapNode.cs Enhances GetVirtualInvokeMapDependencies to handle both generic and non-generic virtual methods, consolidating previously duplicated logic
Comments suppressed due to low confidence (1)

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ReflectionVirtualInvokeMapNode.cs:104

  • [nitpick] The refactoring removes the NeedsVirtualInvokeInfo(factory, slotDefiningMethod) check that existed in the original ReflectedMethodNode.cs code (line 38 of the old version):
if (ReflectionVirtualInvokeMapNode.NeedsVirtualInvokeInfo(factory, slotDefiningMethod) && !factory.VTable(slotDefiningMethod.OwningType).HasKnownVirtualMethodUse)

After refactoring, GetVirtualInvokeMapDependencies only checks NeedsVirtualInvokeInfo(factory, method) before being called (line 83), but not for slotDefiningMethod.

Since FindSlotDefiningMethodForVirtualMethod can return a base class method that may have different devirtualization characteristics (e.g., effectively sealed in a different way), consider whether the check should be preserved:

if (!factory.VTable(slotDefiningMethod.OwningType).HasKnownVirtualMethodUse)
{
    if (NeedsVirtualInvokeInfo(factory, slotDefiningMethod))
    {
        dependencies.Add(factory.VirtualMethodUse(slotDefiningMethod), "Reflection virtual invoke method");
    }
}

Note: The old ReflectionVirtualInvokeMapNode code path (for non-instantiated methods) didn't have this check either, so this may have been redundant defensive programming in ReflectedMethodNode. However, it's worth verifying whether there are edge cases where method needs virtual invoke info but slotDefiningMethod does not.

                else
                {
                    if (!factory.VTable(slotDefiningMethod.OwningType).HasKnownVirtualMethodUse)
                    {
                        dependencies.Add(factory.VirtualMethodUse(slotDefiningMethod), "Reflection virtual invoke method");
                    }

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/ba-g arm64 failure is #121757

@MichalStrehovsky
MichalStrehovsky merged commit bf7fe1d into dotnet:main Nov 18, 2025
@MichalStrehovsky
MichalStrehovsky deleted the reflectmethod branch November 18, 2025 22:33
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants