Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate MethodTables that are/aren't visible to reflection #112782

Merged
merged 5 commits into from
Feb 27, 2025

Conversation

MichalStrehovsky
Copy link
Member

@MichalStrehovsky MichalStrehovsky commented Feb 21, 2025

Fixes #112029.

We use constructed/necessary MethodTables to distinguish between MTs that are visible to user code vs those that aren't. We use unconstructed MTs for things like casts or typeof comparisons, and some of our data structures. We use constructed MTs for everything else. Necessary MT can skip generating some of the data such as GCDesc, vtable, or metadata.

The constructed/necessary MethodTables get reconciled at object writing - if anything needed a constructed one, all references to necessary MTs gets rewritten to point to the constructed one (we never have two MTs for a single type).

Before this PR, we didn't have a "constructed" MT for uninstantiated generic type definitions. That's because those don't have GCDesc or vtable. But they still have metadata, so there is a possible saving.

This introduces a "constructed"/necessary distinction for generic type definition MTs as well. The difference is in the availability of metadata.

It fixes cases like in the referenced bug where using a generic type definition in a typeof comparison (that we normally optimize) doesn't lead to the metadata being optimized away.

Cc @dotnet/ilc-contrib

@Copilot Copilot bot review requested due to automatic review settings February 21, 2025 11:34
@MichalStrehovsky MichalStrehovsky marked this pull request as draft February 21, 2025 11:35
Copy link
Contributor

@Copilot 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.

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/GenericDefinitionEETypeNode.cs:67

  • Confirm that using the 'Marked' property on the constructed type symbol is the correct indicator to decide whether to skip emitting the object node for reflection-invisible generic definitions. An incorrect check here may result in the wrong emission state.
return factory.ConstructedTypeSymbol(_type).Marked;

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs:1138

  • Ensure that comparing 'this' with factory.MaximallyConstructableType(_type) reliably distinguishes between using a constructed versus necessary type symbol. An unexpected outcome here could lead to incorrect dependency emission.
IEETypeNode typeDefNode = factory.MaximallyConstructableType(_type) == this ?

@@ -591,7 +591,7 @@ private IEETypeNode CreateNecessaryTypeNode(TypeDesc type)
{
if (type.IsGenericDefinition)
{
return new GenericDefinitionEETypeNode(this, type);
return new ReflectionInvisibleGenericDefinitionEETypeNode(this, type);
Copy link
Preview

Copilot AI Feb 21, 2025

Choose a reason for hiding this comment

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

Review whether treating all generic definitions as reflection invisible on the necessary type path is intentional. This change could inadvertently hide types intended for reflection, so ensure it aligns with downstream requirements.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky MichalStrehovsky marked this pull request as ready for review February 26, 2025 22:48
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Nice!

@MichalStrehovsky MichalStrehovsky merged commit cd4f82e into dotnet:main Feb 27, 2025
116 of 120 checks passed
@MichalStrehovsky MichalStrehovsky deleted the fix112029 branch February 27, 2025 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ILC fails to detect and trim some dependency cycles (interface -> attribute -> [DAM] -> type impl -> new())
3 participants