-
-
Notifications
You must be signed in to change notification settings - Fork 963
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
Opt out of metadata trimming #2020
Conversation
Benchmark.NET opts itself into trimming, but it's trim compatible only by accident (it's reflecting on things that are statically referenced and therefore would be kept by IL-based trimming solutions). NativeAOT is able to trim metadata more aggressively - e.g. an accessed field might still have its name and metadata removed and the only thing that is kept is a location in memory that can be read and written. I'm now seeing Benchmark.NET failing with latest ILCompiler packages after dotnet/runtime#70201 that enabled more aggressive trimming. Benchmark.NET should ideally `<EnableTrimAnalyzer>true</EnableTrimAnalyzer>` in the BenchmarkDotNet.csproj project and fix all the warnings and the problem will go away. But enabling compat mode should help too.
The failure looks like this. Normally this would be some silent weird behavior (because the field just be missing), but BenchmarkDotNet is opting into
|
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.
LGTM, thank you @MichalStrehovsky !
Another attempt at fixing the problem I tried to fix in dotnet#2020. In that pull request, I reflection-rooted all of BenchmarkDotNet, but that looks like it increases the size of the closure too much (dotnet/performance#2532). So here I'm rolling that part back and annotating enough of the `Characteristic` APIs to make it so that we shouldn't run into the original problem. I basically added `<EnableTrimAnalyzer>true</EnableTrimAnalyzer>` to the BechmarkDotNet project, looked at the trimming warning in https://github.com/dotnet/BenchmarkDotNet/blob/63e28c100a42a6492d09a0b93a8a4c141061bb0d/src/BenchmarkDotNet/Characteristics/CharacteristicHelper.cs#L49-L68 where we were getting the crash and kept getting annotations until it stopped generating new warnings. We now have an annotation in a spot that should make sure the `Job` class has all fields and properties kept (it's used with one of the annotated `Create` methods). There are more trimming warnings that I didn't try to solve because they're unrelated to the problem at hand.
* Make a bit of BenchmarkDotNet trimmable Another attempt at fixing the problem I tried to fix in #2020. In that pull request, I reflection-rooted all of BenchmarkDotNet, but that looks like it increases the size of the closure too much (dotnet/performance#2532). So here I'm rolling that part back and annotating enough of the `Characteristic` APIs to make it so that we shouldn't run into the original problem. I basically added `<EnableTrimAnalyzer>true</EnableTrimAnalyzer>` to the BechmarkDotNet project, looked at the trimming warning in https://github.com/dotnet/BenchmarkDotNet/blob/63e28c100a42a6492d09a0b93a8a4c141061bb0d/src/BenchmarkDotNet/Characteristics/CharacteristicHelper.cs#L49-L68 where we were getting the crash and kept getting annotations until it stopped generating new warnings. We now have an annotation in a spot that should make sure the `Job` class has all fields and properties kept (it's used with one of the annotated `Create` methods). There are more trimming warnings that I didn't try to solve because they're unrelated to the problem at hand.
Benchmark.NET opts itself into trimming, but it's trim compatible only by accident
(it's reflecting on things that are statically referenced and therefore would be kept by IL-based trimming solutions). NativeAOT is able to trim metadata more aggressively - e.g. an accessed field might still have its name and metadata removed and the only thing that is kept is a location in memory that can be read and written.I'm now seeing Benchmark.NET failing with latest ILCompiler packages after dotnet/runtime#70201 that enabled more aggressive trimming.It's reflecting on a field that is not used in any other way than by unanalyzable reflection. We would previously keep all fields, irrespective of whether they're used on reflected on dotnet/runtime#70546.
Benchmark.NET should ideally
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
in the BenchmarkDotNet.csproj project and fix all the warnings and the problem will go away. But enabling compat mode should help too.