Repository navigation
Description
Problem Statement
Today when linking a Xamarin or Blazor application using the dotnet/runtime/src/libraries we are not able to trim a lot of unused code from the libraries. One major reason unused code is left in the libraries is because the libraries are using ILLinkTrim.xml files in order to preserve members that are invoked dynamically across assemblies.
Goals
- Ensure all required code is preserved by the linker.
- Allow Xamarin and Blazor applications to use the
dotnet/runtime/src/librariesand be as small as possible.- Both of these application models are very sensitive to the size of the downloaded application. As such every KB of app size we can reduce is a win for our customers.
Background
Take an example case where AppDomain is calling a private method in System.Security.Claims.
runtime/src/libraries/System.Private.CoreLib/src/System/AppDomain.cs
Lines 389 to 400 in 9af1c5e
| internal IPrincipal? GetThreadPrincipal() | |
| { | |
| IPrincipal? principal = _defaultPrincipal; | |
| if (principal == null) | |
| { | |
| switch (_principalPolicy) | |
| { | |
| case PrincipalPolicy.UnauthenticatedPrincipal: | |
| if (s_getUnauthenticatedPrincipal == null) | |
| { | |
| Type type = Type.GetType("System.Security.Principal.GenericPrincipal, System.Security.Claims", throwOnError: true)!; | |
| MethodInfo? mi = type.GetMethod("GetDefaultInstance", BindingFlags.NonPublic | BindingFlags.Static); |
runtime/src/libraries/System.Security.Claims/src/System/Security/Claims/GenericPrincipal.cs
Lines 86 to 87 in 9af1c5e
| // This is called by AppDomain.GetThreadPrincipal() via reflection. | |
| private static IPrincipal GetDefaultInstance() => new GenericPrincipal(new GenericIdentity(string.Empty), new string[] { string.Empty }); |
A simple usage of Reflection like this can be understood by the mono/linker when both assemblies are given to it at the same time. It sees a hard-coded Type and MethodInfo is being retrieved, and it understands that when AppDomain.GetThreadPrincipal() is called, it has a dependency on GenericPrincipal.GetDefaultInstance(). Thus, when AppDomain.GetThreadPrincipal() must be kept, it must also keep GenericPrincipal.GetDefaultInstance(). And in the reversed case, if an application doesn't need to execute AppDomain.GetThreadPrincipal(), the linker can also safely remove GenericPrincipal.GetDefaultInstance().
For cases that the linker can't figure out itself, we are adding a new attribute DynamicDependency (#30902), which we can place on AppDomain.GetThreadPrincipal() to annotate that it uses Reflection to call GenericPrincipal.GetDefaultInstance(). So even if the linker can't recognize the pattern, we can still be safe.
Today, when we run linker on the dotnet/runtime/src/libraries, we are linking a single assembly at a time. So when we link System.Security.Claims, the linker has no idea that AppDomain.GetThreadPrincipal() requires GenericPrincipal.GetDefaultInstance() (since AppDomain is in a different assembly). To prevent the linker from removing GenericPrincipal.GetDefaultInstance(), we use an ILLinkTrim.xml file that tells the linker to keep the private method. We also then embed the ILLinkTrim.xml file into the assembly so when other apps using System.Security.Claims are linked, the linker will still know that GenericPrincipal.GetDefaultInstance() is needed and not remove it.
The problem is that ILLinkTrim.xml files tell the linker to ALWAYS leave a member in the assembly. It doesn't know WHY the member is needed. Thus, even if the application doesn't use AppDomain.GetThreadPrincipal(), the linker is still leaving in GenericPrincipal.GetDefaultInstance().
Proposed Solution
Instead of linking a single assembly at a time, we should link in the context of the entire "distributable". We can then remove the ILLinkTrim.xml files, and instead add the correct annotations to the code directly. This will give the linker the whole picture of what code is required dynamically by what other code. And when using the library in a different "distributable", the linker can better tell what code can be trimmed safely.
The one "distributable" we are linking for today is the Microsoft.NETCore.App's shared framework. This would mean to link the entire shared framework at the end of our build, and not during each individual library.
One drawback of this approach is that when incrementally building a single library we won't be trimming it anymore during the inner dev loop. It currently takes ~30s to link the entire shared framework, which we wouldn't want to do during the inner dev loop. Unless the linker can support incremental linking, we won't be trimming the individual library. This may result in differences in behavior between the inner dev loop and a full build. Code will be left in the assembly during the inner dev loop that may be trimmed away in the full build. However, these situations wouldn't be able to get past CI since it does a full build.
Alternative Solutions
-
Still link individual assemblies, but only use the
ILLinkTrim.xmlfiles when linking for the shared framework.- This is not ideal because it will lead to duplicate information between the attributes in the code, and the
ILLinkTrim.xml. We will still need to add attributes to the code in order for the libraries to be linker safe when linking for Xamarin applications. And the linker won't be able to see the attributes in other assemblies when linking a single assembly - so we will also need to keep the information in theILLinkTrim.xml.
- This is not ideal because it will lead to duplicate information between the attributes in the code, and the
-
Don't trim the libraries in the shared framework at all.
- If we truly don't care about the size of the shared framework, then we can just turn this functionality off. But completely turning it off would mean a size increase in our distributable. And we wouldn't be able to add more linker optimizations as outlined in Configure illink task to trim unreachable platform-specific code #31785 (although adding more optimizations is not a direct goal of this work).
- With the current
masterbranch here are the size differences in the end shared framework on win-x64:
| Size On Disk | Bytes | |
|---|---|---|
| Trimmed | 65.8 MB | 69,058,560 bytes |
| UnTrimmed | 67.8 MB | 71,118,848 bytes |
- Use "fake" public methods.
- The idea here is if the linker thinks the method is "public", then it won't trim it, even if it doesn't see any calls to it. So we would mark the method "public" in the implementation assembly, but leave it out of the ref assembly.
- This approach honestly feels like a hack and I wouldn't recommend wide usage of it. It also breaks the developer flow of re-generating the ref source file using our tools (
t:GenerateReferenceSource). Now the developer will have to remember to go remove these "fake" public methods in the ref assembly every time it is re-generated.
Original comments
This will allow it to apply more optimizations than when running with reference assemblies (e.g. constant propagation). It'll be required to reduce framework size when built-in substitutions are applied (e.g. intrinsics).
Another benefit we get is that PreserveDependency will work correctly as it will be applied to final assembly, not some intermediate reference.
Activity
stephentoub commentedon Feb 4, 2020
It can absolutely be explored. We just need to be cognizant with such a change of inner-loop development, where a developer iterates on an individual library. If illink doesn't run as part of that, it's more likely we'll see invalid tests and the like get written.
jkotas commentedon Feb 4, 2020
Also, we can do this only for assemblies that ship in netcoreapp and not in a NuGet package.
Can we estimate the benefits so that this can be prioritized against the other work?
marek-safar commentedon Apr 20, 2020
@danmosemsft could you please prioritize this, we'd like to start building in this mode ASAP to test the changes.
danmoseley commentedon Apr 20, 2020
@eerhardt is this something you can help with?
eerhardt commentedon Apr 20, 2020
I'm not sure this is a blocker though. The same situation applies with cross-platform. ex. I have my inner-loop development on Windows, but I just wrote a test that only fails on Linux.
I think it is important that we run our CI tests on the output of the linker, though. That way we ensure what we ship to our customers works the same as what we test.
I looked into it a bit today with the help of @safern @ViktorHofer and @joperezr.
A problem that came up with this work is that there isn't a single place where we can run the linker that will produce netcoreapp assemblies that get picked up for both:
So doing this work with our current infrastructure means we would need to illink twice during our build. Once for the
testhostand again against the shared fx that gets put into the installer/runtime pack. This is very similar to the situation withcrossgentoday. Today we crossgen what gets put into the installer, and #34365 is adding another call tocrossgenthetesthostassemblies before tests are executed.cc @ericstj @janvorli
stephentoub commentedon Apr 20, 2020
It's not a blocker per se, but I don't agree it's the same situation. I can iterate locally and know that the tests are passing on whatever OS I'm working on locally; I can iterate locally on multiple OSes and know the tests are passing on those; etc. With this change, I can iterate locally to my heart's content, but unless I'm doing a full build, I may still hit failures in the tests in CI on that exact OS I was using.
marek-safar commentedon Apr 20, 2020
I think you are on right path except for the fact the illink will be run once as part of the build and then on libraries-illink-test lane as a new testing configuration (this should have been done for 3.1). This is something that could leverage the in-place runtime packs for mobile testing we just added fro Mono runtime or you could write your own version.
jkotas commentedon Apr 20, 2020
We may want to explicitly rethink the approach and guarantees of the dev inner loop, and accept that the inner-loop and whole product build are not the same and you may see a failure in one that you do not see in the other.
This and other issues (#35112) suggest that we should:
eerhardt commentedon Apr 22, 2020
I looked into doing this today, and I think we would only need to link one time during the full build.
My current thinking is the following:
~src/libraries/src.proj, we add a step that will run the linker with arguments like the following (note 2 directories are passed in to-d):$(LibrariesSharedFrameworkBinArtifactsPath)and$(CoreLibLocation)with the trimmed/linked output.~src\libraries\pretest.proj, we copy the trimmed assemblies in$(LibrariesSharedFrameworkBinArtifactsPath)and$(CoreLibLocation)to thetesthostfolder.Now when our tests run, they will run against the trimmed assemblies. And the trimmed assemblies will get picked up in the runtime pack and shared framework installer.
@ViktorHofer @ericstj @stephentoub @jkotas @joperezr @safern @marek-safar - Thoughts?
danmoseley commentedon Apr 22, 2020
[Aside since you mention overwrite, it is generally fragile to overwrite files in a build since if the build doesn't complete successfully the next build can be bad.]
jkotas commentedon Apr 22, 2020
Yep. Do we still have the cases where the same binary ships both in OOB NuGet package and in the shared framework? I assume they would need to be special-cased somehow; or we may just get rid of them completely.
Clr.NativeCoreLibsubset).Agree. Stay aware from overwriting files.
34 remaining items
stephentoub commentedon Apr 27, 2020
Is it really most of them? From #35199 (comment) it looks like it's a minority. Regardless, I'm no fan of the ILLinkTrim.xml files. I'm simply seeing that this cross-assembly private reflection is not a huge problem, showing up in just a handful of cases, so "duplication" to me isn't a huge downside (I'd also love to understand how much trimming these specific methods vs not is impacting app size). And as you noted in your revised intro post above, there are alternatives. For example, an attribute on internal/private members that need to be kept when the library is built (i.e. the first linker pass) but that themselves can be stripped during that build such that the subsequent app-build trimming could shake those out.
All that said, if shared framework size is a goal, then I agree, we should change how we do the build, as we'd have to change something significant about the build anyway (e.g. we don't currently build assemblies other than corelib per architecture).
I would think so, otherwise I question our doing this at all. If it's not, changing the workflow to account for a handful of cases here that could be addressed in other ways seems like overkill to me.
I also pointed out offline to @eerhardt another issue that will need to be addressed as part of this work. Today the linker is used not only for trimming code but also for stripping initlocals. If the linker moves to only be applicable when processing the whole shared framework, then so does its stripping of initlocals, which does have a very measurable impact on perf testing in inner loop for a good number of our assemblies and scenarios. Thus, if we do change to a model where the linker doesn't run as part of inner loop, before committing that change it'll be important to switch the repo to using [SkipLocalsInit]; that's something we should do anyway, but whereas previously it would just have been a nice-to-have, from my perspective doing it must be a precursor to any change here. I opened #35527 for that.
danmoseley commentedon Apr 27, 2020
SkipLocalsInit wouldn't do this either, right? Could we imagine a compiler directive to do it? Those also make lots of noise when we generate the dead code reports.
stephentoub commentedon Apr 27, 2020
Nope. It's unrelated.
danmoseley commentedon Apr 27, 2020
Right... would it be reasonable for the compiler to do it?
marek-safar commentedon Apr 27, 2020
That's being tracked in #35199 and right now it's over 50% for small apps for self-contained setups (this is not a scientific measurement and it's tricky to measure will all the scheduled work).
I think the option of running it at multiple places (during inner library dev loop + during bcl build) is also a possibility.
stephentoub commentedon Apr 27, 2020
Because of the few internal methods being accessed here via cross-assembly reflection?? I'm having a hard time believing that. Is it mainly the XML ones?
marek-safar commentedon Apr 28, 2020
For reference #35547
ericstj commentedon Apr 28, 2020
I just saw the update to the original issue comment and wanted to share a bit of a comment / clarification on my suggestion.
I disagree with this. It is not a hack to declare on an internal or private method that such a method may be called by another assembly. I actually think it is desirable to represent that API as something more than internal/private, which is why I initially suggested maybe making it public would be the right thing to do. If folks don't want to make things public it doesn't seem unreasonable to mark things as preserved in the public case. I honestly think the thing we are doing when we trim the shared framework to publics is a distinctly different task then when we trim an application.
One thing that is interesting is to look at the limited usage of ILLinkTrim in the framework today: https://gist.github.com/ericstj/b7da2181d5f098a7421d425c6667ad16
eerhardt commentedon Apr 28, 2020
At this point, to make forward progress, #35547 is taking the
Alternative Solution 1approach above. While it involves duplication, the limited usage of ILLinkTrim in the framework does make it a feasible approach.If we agree that is a valid solution, we can use the exact same solution for the following assemblies:
After those, the remaining ILLinkTrim.xml usages fall into one of the following buckets:
The first 2 could be eliminated if we added new features to mono/linker. XElement is a special case that might need to be handled specially.
This would allow us to remove all the ILLinkTrim.xml files from the compiled libraries and allow the produced assemblies to be linker safe.
Would moving forward with this plan be acceptable to everyone?
If we later decided to link the entire shared framework en mass during the build, all we would need to do is remove the ILLinkTrim.xml files.
stephentoub commentedon Apr 29, 2020
That sounds like a good path forward to me.
The shared framework size issue can still be followed-up on, but as a separate issue that need not block forward progress here with this approach.
And we can always revise/revert this approach later if we end up doing the whole framework linking, anyway, for shared framework size reasons.
marek-safar commentedon Apr 29, 2020
The bucketing makes sense to me but I disagree that we should use the exact same solution for all the problems (e.g. COM is supported on a small subset of platforms so the solution should reflect that). I'd suggest creating 3 different tracking issues or PRs where we can discuss the right solution for each of them.
eerhardt commentedon May 18, 2020
Done.
eerhardt commentedon May 18, 2020
Since it sounds like the consensus is to not pursue this approach for now, I'm going to close this issue.
To address #35199, we can follow approach
Alternative Solution 1. And for cases that approach won't address, the remaining 3 buckets are tracked by the 3 bugs above.