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

Add more trimming options to TrimMode and change defaults #2856

Merged
merged 14 commits into from
Jul 7, 2022

Conversation

agocke
Copy link
Member

@agocke agocke commented Jun 22, 2022

The new default for trimming is equivalent to TrimmerDefaultAction=link.
Since we're trying to stay away from the term link and TrimmerDefaultAction
isn't a very clear name, this change repurposes TrimMode.

The two new TrimModes are 'full' and 'partial'. The new default is equivalent
to 'TrimMode=full'. These new modes are also recognized for earlier TFMs, but
the defaults are only changed for net7.0+

The new default for trimming is equivalent to TrimmerDefaultAction=link.
Since we're trying to stay away from the term link and TrimmerDefaultAction
isn't a very clear name, this change repurposes TrimMode.

The two new TrimModes are 'full' and 'partial'. The new default is equivalent
to 'TrimMode=full'. These new modes are also recognized for earlier TFMs, but
the defaults are only changed for net7.0+
@agocke
Copy link
Member Author

agocke commented Jun 22, 2022

I pushed a lot of stuff in this change into the task for two reasons: 1) it was getting really complicated to do this in MSBuild properties, and 2) it was much easier to test.

@MichalStrehovsky this will probably look different for NativeAOT, but I haven't looked at the code yet to see how.

MichalStrehovsky added a commit to dotnet/runtime that referenced this pull request Jun 22, 2022
This should root all non-trimmable assemblies, same as TrimmerDefaultAction=copy.

We still support consuming ILCompiler through NuGet when targeting .NET 6, so recognize the old thing too. We might want to cut the .NET 6 support close to RC and at that time TrimmerDefaultAction handling can go away.

Related: dotnet/linker#2856.
Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

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

Looks good to me but I would like Sven to see this too.

Happy to see the TrimMode/TrimmerDefaultAction confusion going away. I kept telling people to forget TrimMode exists. It's now getting a new/better life.

I assume we'll make sure ASP.NET doesn't get insta-broken by this by adding a <TrimMode>partial</> somewhere in their SDK.

src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets Outdated Show resolved Hide resolved
/// Roughly maps to the TargetFrameworkVersion.
/// </summary>
[Required]
public int LinkVersion { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

Works - but I would prefer if we simply passed something like TargetFrameworkVersion - could be the same numerical value, but make it clear that it's the TFM version - I don't want to introduce yet another version number to think about.

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer not to introduce any version to this task. The task should aim to be a straightforward mapping to the command-line arguments. Introducing version-based logic here increases the probability that we set version-specific defaults in the task that can't be overridden by the user.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to make that work, but that means we have to do all of the mapping logic in MSBuild properties. That seems much harder to get right, and also very hard to test

Copy link
Member

Choose a reason for hiding this comment

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

We should have end-to-end tests that exercise the targets+task either way.

It is unfortunately harder to get right. I personally think that is a price worth paying:

  • to make sure we don't break the ability to override defaults, and
  • to keep the task a thin wrapper for the tool (unless we want to introduce TFM-versioned logic to the tool itself).

Comment on lines -54 to +55
<!-- Suppress for WPF/WinForms (unless linking everything) -->
<SuppressTrimAnalysisWarnings Condition="'$(TrimmerDefaultAction)' != 'link' And ('$(UseWpf)' == 'true' Or '$(UseWindowsForms)' == 'true')">true</SuppressTrimAnalysisWarnings>
<!-- Suppress for WPF/WinForms -->
<SuppressTrimAnalysisWarnings Condition="'$(UseWpf)' == 'true' Or '$(UseWindowsForms)' == 'true'">true</SuppressTrimAnalysisWarnings>
Copy link
Member

Choose a reason for hiding this comment

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

Why? If I trim WinForms app and ask for TrimMode=full, I should get all the warnings. Similarly I would expect this behavior in all other places, basically if I say TrimMode=full - I should get warnings by default (since I asked for the "hard " mode).

Copy link
Member Author

Choose a reason for hiding this comment

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

It's currently blocked off completely, which is why we hide the warnings at all. I think we should either show the warnings or disallow trimming.

Copy link
Member

Choose a reason for hiding this comment

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

OK - if we rely on the error for this, then I agree. We should at least add a comment here (why it's OK to suppress). Ideally if the error is disabled we would reenable the warnings (this should really only affect analyzer anyway, the error will trigger before we get to run the linker anyway)

Comment on lines 224 to 226
<!-- In .NET 7, TrimmerDefaultAction is deprecated. TrimMode can be used for the supported configurations. -->
<Warning Condition="'$(TrimmerDefaultAction)' != '' And $([MSBuild]::VersionGreaterThan('$(TargetFrameworkVersion)', '6.0'))"
Text="Property 'TrimmerDefaultAction' is deprecated in .NET 7." />
Copy link
Member

Choose a reason for hiding this comment

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

Please make the warning actionable - maybe just add "Please use TrimMode instead" or something like that.

src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets Outdated Show resolved Hide resolved
@akoeplinger
Copy link
Member

This will also need a change in xamarin-android since they set the TrimmerDefaultAction: https://github.com/xamarin/xamarin-android/blob/2ca2a103d4e473eeeceecd7689961038bb374cdb/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets#L67

/cc @jonathanpeppers

src/ILLink.Tasks/LinkTask.cs Outdated Show resolved Hide resolved
@@ -212,6 +221,10 @@ Copyright (c) .NET Foundation. All rights reserved.
potentially problematic when warnings are suppressed. -->
<NETSdkInformation Condition="'$(PublishTrimmed)' == 'true' And '$(SuppressTrimAnalysisWarnings)' == 'true'" ResourceName="ILLinkOptimizedAssemblies" />

<!-- In .NET 7, TrimmerDefaultAction is deprecated. TrimMode can be used for the supported configurations. -->
<Warning Condition="'$(TrimmerDefaultAction)' != '' And $([MSBuild]::VersionGreaterThan('$(TargetFrameworkVersion)', '6.0'))"
Copy link
Contributor

Choose a reason for hiding this comment

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

@jkotas
Copy link
Member

jkotas commented Jun 22, 2022

Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

It might be worth running the SDK tests locally against this change to catch any breaks before insertion.

/// Roughly maps to the TargetFrameworkVersion.
/// </summary>
[Required]
public int LinkVersion { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer not to introduce any version to this task. The task should aim to be a straightforward mapping to the command-line arguments. Introducing version-based logic here increases the probability that we set version-specific defaults in the task that can't be overridden by the user.

src/ILLink.Tasks/LinkTask.cs Outdated Show resolved Hide resolved
src/ILLink.Tasks/LinkTask.cs Outdated Show resolved Hide resolved
src/ILLink.Tasks/LinkTask.cs Outdated Show resolved Hide resolved
src/ILLink.Tasks/LinkTask.cs Show resolved Hide resolved
@agocke
Copy link
Member Author

agocke commented Jun 22, 2022

In the process of switching over to setting defaults in the targets, but fixing things up in the tasks (with the intent of eventually passing things all the way through to the linker). I still have to set up SDK testing for this.

src/ILLink.Tasks/LinkTask.cs Show resolved Hide resolved
src/ILLink.Tasks/LinkTask.cs Outdated Show resolved Hide resolved
agocke added a commit to agocke/xamarin-android that referenced this pull request Jun 28, 2022
agocke added a commit to agocke/xamarin-macios that referenced this pull request Jun 28, 2022
@rolfbjarne
Copy link
Member

rolfbjarne commented Jul 1, 2022

What's the actual semantics between these modes?

I'm guessing that:

  • full: trims every assembly unless an assembly has opted out.
  • partial: trims every assembly that has opted in.

Is this correct?

@agocke
Copy link
Member Author

agocke commented Jul 1, 2022

Yup that’s right

Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

LGTM, thanks a lot!

@agocke agocke merged commit b6f1fb4 into dotnet:main Jul 7, 2022
@agocke agocke deleted the default-trim-action branch July 7, 2022 18:55
agocke pushed a commit to dotnet/runtime that referenced this pull request Jul 15, 2022
This should root all non-trimmable assemblies, same as TrimmerDefaultAction=copy.

We still support consuming ILCompiler through NuGet when targeting .NET 6, so recognize the old thing too. We might want to cut the .NET 6 support close to RC and at that time TrimmerDefaultAction handling can go away.

Related: dotnet/linker#2856.
jonathanpeppers added a commit to dotnet/android that referenced this pull request Jul 15, 2022
Companion to dotnet/linker#2856

* Update src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets
* Remove more usages of TrimMode
* Update linker versions manually

Co-authored-by: Jonathan Peppers <[email protected]>
jonathanpeppers added a commit to dotnet/android that referenced this pull request Jul 20, 2022
Companion to dotnet/linker#2856

* Update src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets
* Remove more usages of TrimMode
* Update linker versions manually

Co-authored-by: Jonathan Peppers <[email protected]>
jonathanpeppers added a commit to dotnet/android that referenced this pull request Jul 22, 2022
Changes: dotnet/installer@85a0482...a9c056c
Changes: dotnet/linker@ef2d0f2...d27ff61
Changes: dotnet/runtime@206dccb...072eda8
Changes: dotnet/emsdk@40e7c62...11a9acf

Updates:

* Microsoft.Dotnet.Sdk.Internal: from 7.0.100-preview.7.22361.1 to 7.0.100-rc.1.22368.2
* Microsoft.NET.ILLink.Tasks: from 7.0.100-1.22354.1 to 7.0.100-1.22365.1
* Microsoft.NETCore.App.Ref: from 7.0.0-preview.6.22356.1 to 7.0.0-rc.1.22366.5
* Microsoft.NET.Workload.Emscripten.Manifest-7.0.100: from 7.0.0-preview.7.22330.1 to 7.0.0-rc.1.22362.2

~~ Set `$(TrimMode)` `partial` by default (#7132) ~~

Companion to dotnet/linker#2856

* Update src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets
* Remove usages of `%(TrimMode)` for `$(AndroidLinkMode)` of `Full`
* Update .apkdesc files

~~ Set `$(LangVersion)` to 10 in Mono.Android.csproj ~~

There appears to be a C# 11 IL size regression in:

dotnet/roslyn#62832

We can use C# 10 for now to avoid this.

* Fixed `Mono.Android.dll` size in `.apkdesc` files

Co-authored-by: Andy Gocke <[email protected]>
Co-authored-by: Jonathan Peppers <[email protected]>
jonathanpeppers added a commit to dotnet/android that referenced this pull request Jul 22, 2022
Changes: dotnet/installer@d2fff6d...dbdda95
Changes: dotnet/linker@ef2d0f2...33a76b8
Changes: dotnet/runtime@206dccb...db5d4df
Changes: dotnet/emsdk@40e7c62...7d27778

Updates:

* Microsoft.Dotnet.Sdk.Internal: from 7.0.100-preview.7.22362.31 to 7.0.100-preview.7.22370.3
* Microsoft.NET.ILLink.Tasks: from 7.0.100-1.22354.1 to 7.0.100-1.22362.1
* Microsoft.NETCore.App.Ref: from 7.0.0-preview.6.22356.1 to 7.0.0-preview.7.22369.4
* Microsoft.NET.Workload.Emscripten.Manifest-7.0.100: from 7.0.0-preview.7.22330.1 to 7.0.0-preview.7.22361.2

~~ Set `$(TrimMode)` `partial` by default (#7132) ~~

Companion to dotnet/linker#2856

* Update src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets
* Remove usages of `%(TrimMode)` for `$(AndroidLinkMode)` of `Full`
* Update .apkdesc files

~~ Set `$(LangVersion)` to 10 in Mono.Android.csproj ~~

There appears to be a C# 11 IL size regression in:

dotnet/roslyn#62832

We can use C# 10 for now to avoid this.

* Fixed `Mono.Android.dll` size in `.apkdesc` files

Co-authored-by: Andy Gocke <[email protected]>
Co-authored-by: Jonathan Peppers <[email protected]>
agocke added a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…ker#2856)

The new default for trimming is equivalent to TrimmerDefaultAction=link.
Since we're trying to stay away from the term link and TrimmerDefaultAction
isn't a very clear name, this change repurposes TrimMode.

The two new TrimModes are 'full' and 'partial'. The new default is equivalent
to 'TrimMode=full'. These new modes are also recognized for earlier TFMs, but
the defaults are only changed for net7.0+.

Commit migrated from dotnet/linker@b6f1fb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants