From 759b7bef49f72e948a25ed12d343bae167bdc67c Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 29 Feb 2024 15:51:23 +0100 Subject: [PATCH] [dotnet] Merge the 'RemoveAttributesStep' into the 'StoreAttributesStep' Removing (and storing) the attributes earlier make it's possible for them not to be marked. IOW the attribute type itself (and not just the decorations) can be removed. Inspired by / extracted from #14270. --- dotnet/targets/Xamarin.Shared.Sdk.targets | 5 -- .../Steps/PostSweepDispatcher.cs | 13 ---- .../Steps/RemoveAttributesStep.cs | 59 ------------------- .../Steps/StoreAttributesStep.cs | 34 ++++++++++- 4 files changed, 32 insertions(+), 79 deletions(-) delete mode 100644 tools/dotnet-linker/Steps/PostSweepDispatcher.cs delete mode 100644 tools/dotnet-linker/Steps/RemoveAttributesStep.cs diff --git a/dotnet/targets/Xamarin.Shared.Sdk.targets b/dotnet/targets/Xamarin.Shared.Sdk.targets index 2cae73aa144a..b2e8579e5ae7 100644 --- a/dotnet/targets/Xamarin.Shared.Sdk.targets +++ b/dotnet/targets/Xamarin.Shared.Sdk.targets @@ -709,11 +709,6 @@ --> <_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="SweepStep" Type="Xamarin.Linker.ManagedRegistrarLookupTablesStep" Condition="'$(Registrar)' == 'managed-static'" /> - - <_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" AfterStep="SweepStep" Condition="'$(_AreAnyAssembliesTrimmed)' == 'true'" Type="Xamarin.Linker.Steps.PostSweepDispatcher" /> - diff --git a/tools/dotnet-linker/Steps/PostSweepDispatcher.cs b/tools/dotnet-linker/Steps/PostSweepDispatcher.cs deleted file mode 100644 index bb042e6d9be3..000000000000 --- a/tools/dotnet-linker/Steps/PostSweepDispatcher.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Mono.Linker.Steps; -using Xamarin.Linker; - -#nullable enable - -namespace Xamarin.Linker.Steps { - class PostSweepDispatcher : SubStepsDispatcher { - public PostSweepDispatcher () - : base (new [] { new RemoveAttributesStep () }) - { - } - } -} diff --git a/tools/dotnet-linker/Steps/RemoveAttributesStep.cs b/tools/dotnet-linker/Steps/RemoveAttributesStep.cs deleted file mode 100644 index af9acee94086..000000000000 --- a/tools/dotnet-linker/Steps/RemoveAttributesStep.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; - -using Mono.Cecil; - -#nullable enable - -namespace Xamarin.Linker.Steps { - // The .NET linker comes with a way to remove attributes (by passing '--link-attributes - // some.xml' as a command-line argument), but this has a few problems: - // - // * We'd need to figure out which attributes to remove before running the linker, - // but the code to figure out which optimizations have been enabled (and which attributes - // should be removed) is in our custom linker code. We'd need to refactor a big chunk - // of code to move this logic out of our custom linker code. - // * We need to keep the removed attributes around, because the static registrar needs - // them. Our custom linker logic is not notified for removed attributes, which means - // we'd need to store all attributes for the attribute types we're interested in (as - // opposed to this solution, where we only store attributes that are actually removed). - // * The attributes we want removed may contain references to types we don't want - // linked away. If we ask the linker to remove those attributes, then the types may - // be linked away as well, and there's no good way around this. - // - // The end result is that a custom step is the best solution for now. - - public class RemoveAttributesStep : AttributeIteratorBaseStep { - protected override void ProcessAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out bool remove) - { - remove = IsRemovedAttribute (attribute); - - if (remove) - LinkContext.StoreCustomAttribute (provider, attribute); - } - - bool IsRemovedAttribute (CustomAttribute attribute) - { - // this avoids calling FullName (which allocates a string) - var attr_type = attribute.Constructor.DeclaringType; - switch (attr_type.Namespace) { - case Namespaces.ObjCRuntime: - switch (attr_type.Name) { - case "NativeNameAttribute": - return true; - case "AdoptsAttribute": - return LinkContext.App.Optimizations.RegisterProtocols == true; - } - goto default; - case Namespaces.Foundation: - switch (attr_type.Name) { - case "ProtocolAttribute": - case "ProtocolMemberAttribute": - return LinkContext.App.Optimizations.RegisterProtocols == true; - } - goto default; - default: - return false; - } - } - } -} diff --git a/tools/dotnet-linker/Steps/StoreAttributesStep.cs b/tools/dotnet-linker/Steps/StoreAttributesStep.cs index 2af32740f97f..4d9f10e6af5a 100644 --- a/tools/dotnet-linker/Steps/StoreAttributesStep.cs +++ b/tools/dotnet-linker/Steps/StoreAttributesStep.cs @@ -5,7 +5,23 @@ #nullable enable namespace Xamarin.Linker.Steps { - // The registrar needs some of the system attributes that the linker might remove, so store those elsewhere for the static registrar's use. + // The .NET linker comes with a way to remove attributes (by passing '--link-attributes + // some.xml' as a command-line argument), but this has a few problems: + // + // * We'd need to figure out which attributes to remove before running the linker, + // but the code to figure out which optimizations have been enabled (and which attributes + // should be removed) is in our custom linker code. We'd need to refactor a big chunk + // of code to move this logic out of our custom linker code. + // * We need to keep the removed attributes around, because the static registrar needs + // them. Our custom linker logic is not notified for removed attributes, which means + // we'd need to store all attributes for the attribute types we're interested in (as + // opposed to this solution, where we only store attributes that are actually removed). + // * The attributes we want removed may contain references to types we don't want + // linked away. If we ask the linker to remove those attributes, then the types may + // be linked away as well, and there's no good way around this. + // + // The end result is that a custom step is the best solution for now. + public class StoreAttributesStep : AttributeIteratorBaseStep { protected override void ProcessAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out bool remove) { @@ -31,10 +47,24 @@ protected override void ProcessAttribute (ICustomAttributeProvider provider, Cus break; } break; - case "Foundation": + case Namespaces.Foundation: switch (attr_type.Name) { case "ProtocolAttribute": + case "ProtocolMemberAttribute": + store = LinkContext.App.Optimizations.RegisterProtocols == true; + remove = store; + break; + } + break; + case Namespaces.ObjCRuntime: + switch (attr_type.Name) { + case "NativeNameAttribute": + store = true; + remove = store; + break; + case "AdoptsAttribute": store = LinkContext.App.Optimizations.RegisterProtocols == true; + remove = store; break; } break;