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

Support attribute trimming opt-in #1839

Merged
merged 10 commits into from
Feb 25, 2021
Merged

Support attribute trimming opt-in #1839

merged 10 commits into from
Feb 25, 2021

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Feb 17, 2021

@sbomer sbomer changed the title Support attribute trimming opt-in [WIP] Support attribute trimming opt-in Feb 17, 2021
docs/error-codes.md Outdated Show resolved Hide resolved
docs/illink-options.md Outdated Show resolved Hide resolved
src/linker/Linker/LinkContext.cs Outdated Show resolved Hide resolved
Comment on lines 1099 to 1100
Console.WriteLine (" --default-action ACTION Sets action for assemblies that have no trimmability annotation. Defaults to 'link'");
Console.WriteLine (" --action ACTION ASM Overrides the default action for specific assembly name");
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need two options? What about simpler

-action ACTION       Sets action for all assemblies that have no IsTrimmable attribute. Defaults to 'link'
-action ACTION ASM   Overrides the default action for the specific assembly name

Copy link
Member Author

@sbomer sbomer Feb 17, 2021

Choose a reason for hiding this comment

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

I took your naming suggestions. It's a little odd because --action ACTION ASM will override not just the --action ACTION for unattributed assemblies, but also the --trim-mode ACTION for ones with IsTrimmable. In other words, this might give the impression that there's only one "default" action, when really there are two - the --trim-mode and the --action. That's why I initially picked the names --trim-action and --default-action.

However I do like the consistency of your suggestion. Either way we will need to think carefully about the names on the SDK side. The SDK currently uses TrimMode for both the global --trim-mode and the per-assembly --action.

src/linker/Linker/Driver.cs Outdated Show resolved Hide resolved
src/linker/Linker/Driver.cs Outdated Show resolved Hide resolved

namespace Mono.Linker.Steps
{
public class RootNonTrimmableAssemblies : BaseStep
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why this step is needed?

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 needed for example if someone passes -reference UnreferencedAssembly --action copy UnreferencedAssembly - then we need to make sure the unreferenced assembly is kept. This could happen if someone has an unused nuget package dependency (or one that's used via non-understood reflection).

Copy link
Contributor

Choose a reason for hiding this comment

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

Why would we want to do that? It would keep unused assembly which someone redundantly passed as an argument. We have -a option which does the same thing and should be used if it's intentional.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe a better example is -reference UnreferencedAssembly --action copy (setting the default action to copy instead). In this case we would need to root the assembly if it isn't attributed as IsTrimmable.

  • If someone publishes an app with a (statically unreferenced) nuget dependency, without trimming, they would expect to see it in the output. Any non-IsTrimmable assemblies should similarly be kept with PublishTrimmed.
  • We don't want to pass -a for such cases because it would prevent trimming for dependencies that are IsTrimmable.

Then I would do the same with the per-assembly copy action for consistency (this is the first suggestion I made in #1800 (comment)).

src/linker/Linker/AssemblyResolver.cs Outdated Show resolved Hide resolved
src/linker/Linker/LinkContext.cs Outdated Show resolved Hide resolved
src/linker/Linker/LinkContext.cs Outdated Show resolved Hide resolved
@marek-safar
Copy link
Contributor

@sbomer it'd be useful to add the attribute to the runtime build before this change goes in to test it

- Don't warn on duplicate attributes
- Remove comment
- Fix typos and wording
- Use static array
- Rename CheckIsTrimmable -> IsTrimmable
- --trim-action -> --trim-mode
-- --default-action -> --action
@sbomer
Copy link
Member Author

sbomer commented Feb 17, 2021

Currently the tests pass --action skip (or whatever action is specified by SetupLinkerTrimMode in the test) for the framework assemblies, which simulates the attribute. (Skip to avoid copying and analyzing assemblies that are irrelevant to most testcases). Once the attributed framework assemblies flow into the linker we can get rid of this.

@sbomer sbomer changed the title [WIP] Support attribute trimming opt-in Support attribute trimming opt-in Feb 17, 2021
- Add plenty of comments
- RootNonTrimmableAssemblies -> ProcessReferencesStep
- Make -reference order stable using List instead of HashSet
}
}

public static bool IsFullyPreservedAction (AssemblyAction action)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: should be private

Copy link
Member Author

Choose a reason for hiding this comment

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

This one is used also from MarkStep

Copy link
Contributor

@marek-safar marek-safar left a comment

Choose a reason for hiding this comment

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

Please hold the merge after .NET6 P2 SDK is snapped

Make GetInputAssemblyPaths private
@sbomer sbomer merged commit 44907d9 into dotnet:main Feb 25, 2021
sbomer added a commit to sbomer/sdk that referenced this pull request Feb 26, 2021
This implements the SDK side of the behavior described at https://github.com/mono/linker/blob/main/docs/design/trimmed-assemblies.md#net-6.

This includes a linker update with dotnet/linker#1839.
sbomer added a commit to sbomer/sdk that referenced this pull request Feb 26, 2021
This implements the SDK side of the behavior described at https://github.com/mono/linker/blob/main/docs/design/trimmed-assemblies.md#net-6.

This includes a linker update with dotnet/linker#1839.
sbomer added a commit to dotnet/sdk that referenced this pull request Mar 1, 2021
* Enable additional ways to opt in to trimming

This implements the SDK side of the behavior described at https://github.com/mono/linker/blob/main/docs/design/trimmed-assemblies.md#net-6.

This includes a linker update with dotnet/linker#1839.

* PR feedback
- use JoinItems task :)
- always set _TrimmerDefaultAction
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* Support attribute opt-in

* Update docs

* Rename test attributes to match

* Don't pass native SDK assemblies to tests

on Windows

* Update LinkTask

* PR feedback

- Don't warn on duplicate attributes
- Remove comment
- Fix typos and wording
- Use static array
- Rename CheckIsTrimmable -> IsTrimmable

* PR feedback: rename options

- --trim-action -> --trim-mode
-- --default-action -> --action

* Fix ILLink.Tasks test

* PR feedback

- Add plenty of comments
- RootNonTrimmableAssemblies -> ProcessReferencesStep
- Make -reference order stable using List instead of HashSet

* PR feedback

Make GetInputAssemblyPaths private

Commit migrated from dotnet/linker@44907d9
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.

2 participants