Skip to content

Commit

Permalink
NativeAOT: Disable AggressiveAttributeTrimming with ILLink (xamarin#1…
Browse files Browse the repository at this point in the history
…8545)

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
dotnet/runtime#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: xamarin#18479

---------

Co-authored-by: Alex Soto <alex@alexsoto.me>
  • Loading branch information
ivanpovazan and dalexsoto committed Jul 17, 2023
1 parent f212f6b commit b68efbe
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@

<ProjectCapability Condition="'$(_KeepLaunchProfiles)' != 'true'" Remove="LaunchProfiles" />
</ItemGroup>


<!-- Default item includes (globs and implicit references) -->
<Import Project="Xamarin.Shared.Sdk.DefaultItems.targets" />

<PropertyGroup>
<!-- Add a property that specifies the name of the platform assembly for each platform -->
<_PlatformAssemblyName>Microsoft.$(_PlatformName)</_PlatformAssemblyName>
Expand Down Expand Up @@ -132,7 +135,6 @@
<UseSystemResourceKeys Condition="'$(UseSystemResourceKeys)' == '' And '$(_BundlerDebug)' == 'true'">false</UseSystemResourceKeys>
<UseNativeHttpHandler Condition="'$(_PlatformName)' != 'macOS' And '$(UseNativeHttpHandler)' == ''">true</UseNativeHttpHandler>
<!-- AutoreleasePoolSupport needs to be set earlier, so that illink doesn't override it - https://github.com/dotnet/runtime/pull/86753 - so it's set in Xamarin.Shared.Sdk.props -->
<_AggressiveAttributeTrimming Condition="'$(_AggressiveAttributeTrimming)' == ''">true</_AggressiveAttributeTrimming>
<NullabilityInfoContextSupport Condition="'$(NullabilityInfoContextSupport)' == ''">false</NullabilityInfoContextSupport>
<BuiltInComInteropSupport Condition="'$(BuiltInComInteropSupport)' == ''">false</BuiltInComInteropSupport>
<!-- Verify DI trimmability at development-time, but turn the validation off for production/trimmed builds. -->
Expand All @@ -141,6 +143,15 @@

<!-- We don't need to generate reference assemblies for apps or app extensions -->
<ProduceReferenceAssembly Condition="'$(ProduceReferenceAssembly)' == '' And ('$(OutputType)' == 'Exe' Or '$(IsAppExtension)' == 'true')">false</ProduceReferenceAssembly>

<!--
With NativeAOT we want to prevent ILLink from removing attributes like `IsTrimmable` so further trimming can be done by the NativeAOT toolchain.
For this reason, in case of NativeAOT, we set _AggressiveAttributeTrimming to false by default and store the overwriten default in
_OriginalAggressiveAttributeTrimming property, which is later used to properly configure NativeAOT trimming.
-->
<_OriginalAggressiveAttributeTrimming>$(_AggressiveAttributeTrimming)</_OriginalAggressiveAttributeTrimming>
<_AggressiveAttributeTrimming Condition="'$(_UseNativeAot)' == 'true'">false</_AggressiveAttributeTrimming>
<_AggressiveAttributeTrimming Condition="'$(_AggressiveAttributeTrimming)' == ''">true</_AggressiveAttributeTrimming>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -152,9 +163,6 @@
<TargetPlatformSupported Condition=" '$(TargetPlatformIdentifier)' == '$(_PlatformName)' ">true</TargetPlatformSupported>
</PropertyGroup>

<!-- Default item includes (globs and implicit references) -->
<Import Project="Xamarin.Shared.Sdk.DefaultItems.targets" />

<PropertyGroup Condition="'$(_GlobalizationDataFileLocation)' == ''">
<_GlobalizationDataFileLocation Condition="'$(_UseNativeAot)' == 'true'">Resource</_GlobalizationDataFileLocation>
<_GlobalizationDataFileLocation Condition="'$(_UseNativeAot)' != 'true'">Assembly</_GlobalizationDataFileLocation>
Expand Down Expand Up @@ -1216,6 +1224,19 @@

<!-- Link in the output from ILC -->
<_NativeExecutableObjectFiles Include="$(NativeObject)" />

<!-- By default perform aggressive attribute trimming, except when explicitly disabled -->
<_TrimmerFeatureSettings Condition="'$(_OriginalAggressiveAttributeTrimming)' != 'false' and '%(Identity)' == 'System.AggressiveAttributeTrimming'">
<Value>true</Value>
<Trim>true</Trim>
</_TrimmerFeatureSettings>
<RuntimeHostConfigurationOption Condition="'$(_OriginalAggressiveAttributeTrimming)' != 'false' and '%(Identity)' == 'System.AggressiveAttributeTrimming'">
<Value>true</Value>
<Trim>true</Trim>
</RuntimeHostConfigurationOption>

<!-- Explicitly root the framework assembly due to NativeAOT trimming incompatibility tracked: https://github.com/dotnet/runtime/issues/86649 -->
<TrimmerRootAssembly Include="$(_PlatformAssemblyName)" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit b68efbe

Please sign in to comment.