Skip to content

Conversation

MichalStrehovsky
Copy link
Member

@MichalStrehovsky MichalStrehovsky commented Aug 12, 2025

  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, 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 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.

Cc @dotnet/ilc-contrib

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.
@Copilot Copilot AI review requested due to automatic review settings August 12, 2025 14:54
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 encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Aug 13, 2025
@MichalStrehovsky MichalStrehovsky merged commit d00e383 into dotnet:main Aug 13, 2025
104 of 107 checks passed
@MichalStrehovsky MichalStrehovsky deleted the trimattributes branch August 13, 2025 22:06
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.
@github-actions github-actions bot locked and limited conversation to collaborators Sep 13, 2025
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants