Skip to content

Conversation

MichalStrehovsky
Copy link
Member

(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 #118640 can fully kick in because enums are the last remaining place that looks at custom attributes (looking for FlagsAttribute).

Cc @dotnet/ilc-contrib

(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`).
@Copilot Copilot AI review requested due to automatic review settings August 14, 2025 06:12
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.

Pull Request Overview

This PR aims to reduce the size of hello world applications to 745 kB by eliminating enum boxing and expensive enum stringification logic. The changes focus on optimizing enum handling in the NativeAOT runtime to enable better size optimizations.

Key changes:

  • Avoids enum boxing by using direct value comparisons and explicit casts
  • Replaces enum ToString() calls with hardcoded string switch expressions
  • Refactors methods to bypass enum validation when internal consistency is guaranteed

Reviewed Changes

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

Show a summary per file
File Description
CultureData.cs Replaces enum array initialization with cast from ushort array to avoid boxing
Thread.NativeAot.Windows.cs Changes method visibility and replaces enum ToString() with switch expression
GC.NativeAot.cs Refactors to bypass enum validation in internal calls
MethodTable.Runtime.cs Optimizes enum detection logic using direct type comparison
StartupCodeHelpers.Extensions.cs Uses internal unchecked method to avoid enum validation overhead

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Copy link
Contributor

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

@jkotas
Copy link
Member

jkotas commented Aug 14, 2025

This looks reasonable to me. The unnatural patterns need comments to make it clear why they are needed.

@jkotas jkotas requested a review from tarekgh August 15, 2025 08:35
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 jkotas merged commit c277b32 into dotnet:main Aug 15, 2025
139 of 144 checks passed
@MichalStrehovsky MichalStrehovsky deleted the drophello branch August 15, 2025 20:30
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 15, 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.

5 participants