From b68efbed4a1275ac49bb7b2d8ab9bd3dfd64ef35 Mon Sep 17 00:00:00 2001 From: Ivan Povazan <55002338+ivanpovazan@users.noreply.github.com> Date: Mon, 17 Jul 2023 15:26:01 +0200 Subject: [PATCH] NativeAOT: Disable AggressiveAttributeTrimming with ILLink (#18545) In the current setup with NativeAOT, during app build we run both ILLink and ILCompiler in cascade. When `_AggressiveAttributeTrimming` feature switch is set to `true` ILLink removes `IsTrimmable` attribute from assemblies, which means that when we are also running in `TrimMode=partial` (which translates to `--defaultrooting` ILC command-line arguments) ILC trimming is disabled completely. This PR disables `_AggressiveAttributeTrimming` in the first pass ie during trimming by ILLink and enables it in the second trimming pass performed by ILCompiler. Additionally, to workaround ILCompiler incompatibility with `Microsoft.iOS` (and friends) this platform assembly is explicitly rooted when passed to ILCompiler for trimming (this will be fixed once https://github.com/dotnet/runtime/issues/86649 is resolved). Estimated savings: This change reduces the size of the application bundle by `0,58Mb` (or ~4,3% compared to the baseline) | MAUI ios app | Base | This PR | diff (%) | |--------------|-----------|-----------|----------| | SOD (Mb) | 41,93 | 40,5 | -3,4% | | .ipa (Mb) | 13,43 | 12,85 | -4,3% | Fixes: https://github.com/xamarin/xamarin-macios/issues/18479 --------- Co-authored-by: Alex Soto --- dotnet/targets/Xamarin.Shared.Sdk.targets | 31 +++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/dotnet/targets/Xamarin.Shared.Sdk.targets b/dotnet/targets/Xamarin.Shared.Sdk.targets index 3043880a9344..7e5d8c983144 100644 --- a/dotnet/targets/Xamarin.Shared.Sdk.targets +++ b/dotnet/targets/Xamarin.Shared.Sdk.targets @@ -96,7 +96,10 @@ - + + + + <_PlatformAssemblyName>Microsoft.$(_PlatformName) @@ -132,7 +135,6 @@ false true - <_AggressiveAttributeTrimming Condition="'$(_AggressiveAttributeTrimming)' == ''">true false false @@ -141,6 +143,15 @@ false + + + <_OriginalAggressiveAttributeTrimming>$(_AggressiveAttributeTrimming) + <_AggressiveAttributeTrimming Condition="'$(_UseNativeAot)' == 'true'">false + <_AggressiveAttributeTrimming Condition="'$(_AggressiveAttributeTrimming)' == ''">true @@ -152,9 +163,6 @@ true - - - <_GlobalizationDataFileLocation Condition="'$(_UseNativeAot)' == 'true'">Resource <_GlobalizationDataFileLocation Condition="'$(_UseNativeAot)' != 'true'">Assembly @@ -1216,6 +1224,19 @@ <_NativeExecutableObjectFiles Include="$(NativeObject)" /> + + + <_TrimmerFeatureSettings Condition="'$(_OriginalAggressiveAttributeTrimming)' != 'false' and '%(Identity)' == 'System.AggressiveAttributeTrimming'"> + true + true + + + true + true + + + +