-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Trim custom attributes #118640
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
Merged
MichalStrehovsky
merged 2 commits into
dotnet:main
from
MichalStrehovsky:trimattributes
Aug 13, 2025
Merged
Trim custom attributes #118640
MichalStrehovsky
merged 2 commits into
dotnet:main
from
MichalStrehovsky:trimattributes
Aug 13, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. To be able to read custom attributes at runtime, we need to use the internal metadata APIs 2. When the internal metadata API is not used, we know nobody could be reading the attributes 3. Make custom attribute emission conditional on the presence of the reading API in the graph 4. Use separate conditions for different kinds of attributes - fields, types, methods, etc. In practice, this doesn't help much outside of Hello World scenarios because custom attribute reading at runtime happens through a virtual method on `MemberInfo`. So if e.g. `PropertyInfo` is allocated and somebody calls `Type.GetCustomAttributes` (which is `MemberInfo.GetCustomAttributes` virtual), we include code to read attributes on properties too. This is a solvable problem if we learn to do devirtualization during scanning phase; then we can reap more benefit from this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
This was referenced Aug 12, 2025
jkotas
reviewed
Aug 12, 2025
...t/ILCompiler.Compiler/Compiler/DependencyAnalysis/CustomAttributeBasedDependencyAlgorithm.cs
Show resolved
Hide resolved
jkotas
approved these changes
Aug 12, 2025
MichalStrehovsky
added a commit
to MichalStrehovsky/runtime
that referenced
this pull request
Aug 14, 2025
(With `StackTraceSupport=false`, `OptimizationPreference=Size`, `UseSystemResourceKeys=true`) I don't know if we want it in this form, but it is tempting. The thing that makes the difference is that we are no longer boxing enums and we can get rid of the super expensive enum stringification logic. Not boxing enums also means that dotnet#118640 can fully kick in because enums are the last remaining place that looks at custom attributes (looking for `FlagsAttribute`).
MichalStrehovsky
added a commit
to MichalStrehovsky/runtime
that referenced
this pull request
Aug 15, 2025
…lysis A couple times in the past I needed a way to say "if X is not part of the program, eliminate the entire basic block". We can do this for allocated types (this is how branches under `is` checks elimination works), but we can't do this for more general "characteristics". This introduces a mechanism where AOT compiler and CoreLib (or System.Private.* universe in general) can define whole program tags such as "whole program has X in it" and CoreLib can condition code on the presence of this tag. This is easier shown than described, so I extracted the first use of this into a separate commit. In this commit, we eliminate code that tries looking for `StackTraceHiddenAttribute` if we know the whole program has no `StackTraceHiddenAttribute` in it. With this code eliminated, dotnet#118640 can then eliminate all custom attributes on methods, which in turn plays into dotnet#118718 and we can eliminate enum boxing even when StackTraceSupport is not set to false (right now dotnet#118718 really needs the StackTraceSupport=false to get rid of boxed enums; we get more boxed enums from method attributes). We have a new node that represents the characteristic. The node can be dropped into the graph wherever needed. ILScanner then uses this to condition parts of the method body on this characteristic node. We need similar logic in the substitution IL provider because we need to guarantee that RyuJIT is not going to see basic blocks we didn't scan. So we treat it as a substitution during codegen phase too.
jkotas
added a commit
that referenced
this pull request
Aug 15, 2025
(With `StackTraceSupport=false`, `OptimizationPreference=Size`, `UseSystemResourceKeys=true`) The thing that makes the difference is that we are no longer boxing enums and we can get rid of the super expensive enum stringification logic. Not boxing enums also means that #118640 can fully kick in because enums are the last remaining place that looks at custom attributes (looking for `FlagsAttribute`). Co-authored-by: Jan Kotas <[email protected]>
MichalStrehovsky
added a commit
that referenced
this pull request
Aug 15, 2025
…lysis (#118769) A couple times in the past I needed a way to say "if X is not part of the program, eliminate the entire basic block". We can do this for allocated types (this is how branches under `is` checks elimination works), but we can't do this for more general "characteristics". This introduces a mechanism where AOT compiler and CoreLib (or System.Private.* universe in general) can define whole program tags such as "whole program has X in it" and CoreLib can condition code on the presence of this tag. This is easier shown than described, so I extracted the first use of this into a separate commit. In this commit, we eliminate code that tries looking for `StackTraceHiddenAttribute` if we know the whole program has no `StackTraceHiddenAttribute` in it. With this code eliminated, #118640 can then eliminate all custom attributes on methods, which in turn plays into #118718 and we can eliminate enum boxing even when StackTraceSupport is not set to false (right now #118718 really needs the StackTraceSupport=false to get rid of boxed enums; we get more boxed enums from method attributes). We have a new node that represents the characteristic. The node can be dropped into the graph wherever needed. ILScanner then uses this to condition parts of the method body on this characteristic node. We need similar logic in the substitution IL provider because we need to guarantee that RyuJIT is not going to see basic blocks we didn't scan. So we treat it as a substitution during codegen phase too.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
area-NativeAOT-coreclr
linkable-framework
Issues associated with delivering a linker friendly framework
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In practice, I don't expect it to help much outside of Hello World scenarios because custom attribute reading at runtime happens through a virtual method on
MemberInfo
. So if e.g.PropertyInfo
is allocated and somebody callsType.GetCustomAttributes
(which isMemberInfo.GetCustomAttributes
virtual), we include code to read attributes on properties too. This is a solvable problem if we learn to do devirtualization during scanning phase; then we can reap more benefit from this.Cc @dotnet/ilc-contrib